python中multiple函数_关于多处理:在Python中将多个参数传递给pool.map()函数
本問題已經(jīng)有最佳答案,請(qǐng)猛點(diǎn)這里訪問。
我需要一些方法來使用pool.map()中接受多個(gè)參數(shù)的函數(shù)。根據(jù)我的理解,pool.map()的目標(biāo)函數(shù)只能有一個(gè)iterable作為參數(shù),但是有沒有其他參數(shù)也可以傳入的方法?在這種情況下,我需要傳遞一些配置變量,比如鎖(),并將信息記錄到目標(biāo)函數(shù)中。
我試過做一些研究,我認(rèn)為我可以使用部分函數(shù)使它工作?但是我不完全理解這些是如何工作的。任何幫助都將不勝感激!下面是一個(gè)簡單的例子,說明我想做什么:
def target(items, lock):
for item in items:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
pool.map(target(PASS PARAMS HERE), iterable)
pool.close()
pool.join()
這里討論:stackoverflow.com/questions/5442910/…(我成功地使用了J.F.Sebastien的"star"方法)
請(qǐng)?jiān)谑褂枚噙M(jìn)程時(shí)使用try/finally子句,finally中包含close()和join(),以確保在發(fā)生錯(cuò)誤時(shí)關(guān)閉進(jìn)程。stackoverflow.com/questions/30506489/…
您可以使用EDOCX1[0]進(jìn)行此操作(如您懷疑的那樣):
from functools import partial
def target(lock, iterable_item):
for item in iterable_item:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
l = multiprocessing.Lock()
func = partial(target, l)
pool.map(func, iterable)
pool.close()
pool.join()
例子:
def f(a, b, c):
print("{} {} {}".format(a, b, c))
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
a ="hi"
b ="there"
func = partial(f, a, b)
pool.map(func, iterable)
pool.close()
pool.join()
if __name__ =="__main__":
main()
號(hào)
輸出:
hi there 1
hi there 2
hi there 3
hi there 4
hi there 5
太棒了,我想我所需要的就是一個(gè)這樣清晰的例子。非常感謝!
很好的例子。但有一個(gè)問題:為什么在target的定義中有for item in items:?
@讓·弗朗索瓦。復(fù)制/粘貼錯(cuò)誤!謝謝你指出。
那些對(duì)鎖有問題的人,請(qǐng)查看stackoverflow.com/questions/25557686/…
如果變量在第一個(gè)位置呢?如test(input, p1, p2, p3=None),我有p1, p2, p3固定,input變化?
@Mithril寫了一個(gè)這樣的頂級(jí)函數(shù):def mytest(p1, p2, input, p3=None): test(input, p1, p2, p3=None),然后做func = partial(mytest, some_p1, some_p2, p3=some_p3),把func傳遞給pool.map。
您可以使用允許多個(gè)參數(shù)的map函數(shù),就像在pathos中找到的multiprocessing的分叉一樣。
>>> from pathos.multiprocessing import ProcessingPool as Pool
>>>
>>> def add_and_subtract(x,y):
... ? return x+y, x-y
...
>>> res = Pool().map(add_and_subtract, range(0,20,2), range(-5,5,1))
>>> res
[(-5, 5), (-2, 6), (1, 7), (4, 8), (7, 9), (10, 10), (13, 11), (16, 12), (19, 13), (22, 14)]
>>> Pool().map(add_and_subtract, *zip(*res))
[(0, -10), (4, -8), (8, -6), (12, -4), (16, -2), (20, 0), (24, 2), (28, 4), (32, 6), (36, 8)]
pathos使您能夠輕松地用多個(gè)輸入嵌套分層并行映射,因此我們可以擴(kuò)展我們的示例來證明這一點(diǎn)。
>>> from pathos.multiprocessing import ThreadingPool as TPool
>>>
>>> res = TPool().amap(add_and_subtract, *zip(*Pool().map(add_and_subtract, range(0,20,2), range(-5,5,1))))
>>> res.get()
[(0, -10), (4, -8), (8, -6), (12, -4), (16, -2), (20, 0), (24, 2), (28, 4), (32, 6), (36, 8)]
。
更有趣的是,構(gòu)建一個(gè)可以傳遞到池中的嵌套函數(shù)。這是可能的,因?yàn)閜athos使用dill,它可以序列化Python中的幾乎所有內(nèi)容。
>>> def build_fun_things(f, g):
... ? def do_fun_things(x, y):
... ? ? return f(x,y), g(x,y)
... ? return do_fun_things
...
>>> def add(x,y):
... ? return x+y
...
>>> def sub(x,y):
... ? return x-y
...
>>> neato = build_fun_things(add, sub)
>>>
>>> res = TPool().imap(neato, *zip(*Pool().map(neato, range(0,20,2), range(-5,5,1))))
>>> list(res)
[(0, -10), (4, -8), (8, -6), (12, -4), (16, -2), (20, 0), (24, 2), (28, 4), (32, 6), (36, 8)]
。
但是,如果您不能走出標(biāo)準(zhǔn)庫,您將不得不以另一種方式進(jìn)行。在這種情況下,最好的選擇是使用multiprocessing.starmap,如這里所示:python multiprocessing pool.map用于多個(gè)參數(shù)(由@roberto在OP文章的注釋中指出)
獲取pathos:https://github.com/uqfoundation
如果您沒有訪問functools.partial的權(quán)限,也可以為此使用包裝函數(shù)。
def target(lock):
def wrapped_func(items):
for item in items:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
return wrapped_func
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
lck = multiprocessing.Lock()
pool.map(target(lck), iterable)
pool.close()
pool.join()
。
這使得target()成為一個(gè)接受鎖的函數(shù)(或任何你想給出的參數(shù)),它將返回一個(gè)只接受一個(gè)iterable作為輸入的函數(shù),但仍然可以使用你的所有其他參數(shù)。這就是最終傳遞給pool.map()的內(nèi)容,然后應(yīng)該毫無問題地執(zhí)行。
我已經(jīng)非常晚了,但這段代碼不起作用,因?yàn)榍短缀瘮?shù)不能被pickle。調(diào)用target(lck)將返回嵌套的wrapped_func函數(shù),該函數(shù)需要經(jīng)過處理才能傳遞給工作進(jìn)程,并且總是失敗。
總結(jié)
以上是生活随笔為你收集整理的python中multiple函数_关于多处理:在Python中将多个参数传递给pool.map()函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为错误报告在哪个文件夹_华为手机隐藏的
- 下一篇: websocket python爬虫_p