并行的Python:我如何提供参数“提交”?
-
20-09-2019 - |
题
这是仅与平行蟒标签的第二个问题。翻翻文件和google搜索的主题后,我来到这里,因为它是在那里我有答案和建议好运。
以下是API(我认为这就是所谓的),其提交所有相关信息,以页
def submit(self, func, args=(), depfuncs=(), modules=(),
callback=None, callbackargs=(), group='default', globals=None):
"""Submits function to the execution queue
func - function to be executed
args - tuple with arguments of the 'func'
depfuncs - tuple with functions which might be called from 'func'
modules - tuple with module names to import
callback - callback function which will be called with argument
list equal to callbackargs+(result,)
as soon as calculation is done
callbackargs - additional arguments for callback function
group - job group, is used when wait(group) is called to wait for
jobs in a given group to finish
globals - dictionary from which all modules, functions and classes
will be imported, for instance: globals=globals()
"""
下面是我提交语句,它的参数:
job_server.submit(reify, (pop1, pop2, 1000),
depfuncs = (key_seq, Chromosome, Params, Node, Tree),
modules = ("math",),
callback = sum.add, globals = globals())
所有在depfuncs
大写名称的类的名称。我不知道放在哪里类或即使我需要包括他们,因为他们是在globals()
字典。但是,当我与depfuncs()
运行它空的,它会产生一个错误,如“Tree not defined
”(例如)。
现在,key_seq
是发电机,所以我有,以便与它的一个实例的工作,以便能够使用.next()
:
def key_seq():
a = 0
while True:
yield a
a = a + 1
ks = key_seq()
ks
在globals()
定义。当我不包括任何其他地方,我得到了一个错误说“ks is not defined
”。
当我包括ks
depfuncs
,这是错误:
Traceback (most recent call last):
File "C:\Python26\Code\gppp.py", line 459, in <module>
job_server.submit(reify, (pop1, pop2, 1000), depfuncs = (key_seq, ks, Chromosome, Params, Node, Tree), modules = ("math",), callback = sum.add, globals = globals())
File "C:\Python26\lib\site-packages\pp.py", line 449, in submit
sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)
File "C:\Python26\lib\site-packages\pp.py", line 634, in __dumpsfunc
sources = [self.__get_source(func) for func in funcs]
File "C:\Python26\lib\site-packages\pp.py", line 713, in __get_source
sourcelines = inspect.getsourcelines(func)[0]
File "C:\Python26\lib\inspect.py", line 678, in getsourcelines
lines, lnum = findsource(object)
File "C:\Python26\lib\inspect.py", line 519, in findsource
file = getsourcefile(object) or getfile(object)
File "C:\Python26\lib\inspect.py", line 441, in getsourcefile
filename = getfile(object)
File "C:\Python26\lib\inspect.py", line 418, in getfile
raise TypeError('arg is not a module, class, method, '
TypeError: arg is not a module, class, method, function, traceback, frame, or code object
我敢肯定arg
指的是ks
。所以,在这里我告诉.submit()
ks
?我不明白什么是应该去的地方。感谢。
解决方案
有趣的 - 你在干什么遗传学模拟?我问,因为我在那里看到“染色体”,我曾经使用并行蟒开发了群体遗传学仿真。
你的方法看起来很复杂。在我的并行蟒程序,我使用的下列调用:
job = jobServer.submit( doRun, (param,))
我怎么逃脱呢?诀窍是,doRun功能不会在相同的上下文中,你打电话透过上下文中运行。例如(人为的例子):
import os, pp
def doRun(param):
print "your name is %s!" % os.getlogin()
jobServer = pp.Server()
jobServer.submit( doRun, (param,))
这个代码将失败。这是因为操作系统模块不存在doRun - doRun不在同一上下文中运行提交。当然,你可以在os
的module
参数传递submit
,但不是很容易,只为了呼唤import os
在doRun?
平行蟒试图避免python的GIL通过在一个完全独立的进程中运行的功能。它试图使这个更容易让你举“通行证”参数和命名空间给你的函数下咽,但它确实用这个黑客。例如,你的类将使用pickle
的一些变体中被串行化,然后解序列化在新的过程。
但不是依靠submit
的黑客,只是接受你的功能将需要做设置它的运行范围内的一切工作的现实。你真的有两个main
功能 - 一个建立呼叫到submit
,以及一个,你通过submit
,这实际上设置你需要做的工作调用。
如果你从你的产生需要的下一个值是可用于PP运行,也把它作为一个参数!这避免了lambda函数和发电机参考,并留下你传递一个简单的变量!
我的代码不再被维护,但如果你很好奇看看这里: HTTP://pps-spud.uchicago。 EDU / viewvc / FPS /中继/蟒/ fps.py?视图=标记
其他提示
我觉得你应该传递拉姆达:ks.next()而不是普通的老KS