并行Python:如何为'submit'提供参数?

Pet*_*art 6 python parallel-python

这只是parallel-python标签的第二个问题.通过浏览文档和谷歌搜索主题后,我来到这里,因为它是我得到答案和建议最好的运气.

以下是将所有相关信息提交给pp的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()
    """
Run Code Online (Sandbox Code Playgroud)

这是我的提交声明及其参数:

job_server.submit(reify, (pop1, pop2, 1000), 
                  depfuncs = (key_seq, Chromosome, Params, Node, Tree), 
                  modules = ("math",), 
                  callback = sum.add, globals = globals())
Run Code Online (Sandbox Code Playgroud)

所有大写的名称depfuncs都是类的名称.我不知道在哪里放类,或者即使我需要将它们包括在globals()字典中.但是当我用depfuncs()空运行它时,它会引发一个错误,例如" Tree not defined"(例如).

现在,key_seq是一个生成器,所以我必须使用它的一个实例才能使用.next():

def key_seq():
    a = 0
    while True:
        yield a
        a = a + 1
ks = key_seq()
Run Code Online (Sandbox Code Playgroud)

ks定义于globals().当我没有把它包括在其他任何地方时,我得到一个错误说' ks is not defined'.当我加入ksdepfuncs,这是错误:

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
Run Code Online (Sandbox Code Playgroud)

我很确定这arg是指ks.那么,我在哪里.submit()讲述ks?我不明白应该去哪里.谢谢.

Igo*_*any 5

有趣的是 - 你在进行遗传学模拟吗?我问,因为我在那里看到'染色体',我曾经使用平行python开发了一个群体遗传模拟.

你的方法看起来很复杂.在我的并行python程序中,我使用了以下调用:

job = jobServer.submit( doRun, (param,))
Run Code Online (Sandbox Code Playgroud)

我是怎么逃避这个的?诀窍是doRun函数不会在与调用sumbit的上下文相同的上下文中运行.例如(人为的例子):

import os, pp

def doRun(param):
    print "your name is %s!" % os.getlogin()

jobServer = pp.Server()
jobServer.submit( doRun, (param,))
Run Code Online (Sandbox Code Playgroud)

这段代码会失败.这是因为doRun中不存在os模块 - doRun不在与submit相同的上下文中运行.当然,你可以通过osmodule参数submit,不过,是不是更容易只是呼吁import os在doRun?

parallel python试图通过在一个完全独立的进程中运行你的函数来避免python的GIL.它试图通过让你引用"传递"参数和命名空间到你的函数来使这更容易吞下,但它使用hacks来做到这一点.例如,您的类将使用pickle新进程中的某些变体序列化,然后在新进程中反序列化.

但是,不要依赖于submit黑客,只要接受你的功能将需要完成设置它的运行上下文的所有工作的现实.你真的有两个main功能 - 一个用于设置呼叫submit,一个用于调用submit,实际上是你需要做的工作.

如果您需要生成器的下一个值可用于pp运行,也可以将其作为参数传递!这避免了lambda函数和生成器引用,并让你传递一个简单的变量!

我的代码不再被维护,但如果你很好奇看看这里: http://pps-spud.uchicago.edu/viewvc/fps/trunk/python/fps.py?view=markup