Parallel Python: Passing a function written in another module to 'submit'
我使用的是并行的python模块(
1 2 3 4 5 6 7 | import pp from walkerc import walk # Other stuff here ser = pp.Server() # Some more definitions ser.submit(walk, (it, params)) ser.submit(walk, (1000, params), modules = ("walkerc",), globals = globals()) |
上述两个语句都失败,我得到以下错误:
Traceback (most recent call last):
File"", line 1, in
ser.submit(walk, (1000, params), modules = ("walkerc",), globals = globals())File"/usr/lib/python2.7/site-packages/pp.py", line 458, in submit
sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)File"/usr/lib/python2.7/site-packages/pp.py", line 629, in
__dumpsfunc
sources = [self.__get_source(func) for func in funcs]File"/usr/lib/python2.7/site-packages/pp.py", line 696, in
__get_source
sourcelines = inspect.getsourcelines(func)[0]File"/usr/lib/python2.7/inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)File"/usr/lib/python2.7/inspect.py", line 526, in findsource
file = getfile(object)File"/usr/lib/python2.7/inspect.py", line 420, in getfile
'function, traceback, frame, or code object'.format(object))TypeError: '<'built-in function walk'>' is not a module, class, method,
function, traceback, frame, or code object
号
函数"walk"本身是在主程序中正确导入的,它是将其提交给新工作人员的过程,这是有问题的。
如何正确指定函数名"walk"?我不想在我称之为"walk"的同一个文件中定义"walk",因为我已经在cython中对其进行了修改,并希望有更好的性能。还有其他选择吗?
尝试将您的
我可以在我的系统上成功地传递这样导入的
1 2 3 4 5 | import pp from walkerc import walk pps = pp.Server() pps.submit(walk, args=(1,)) |
但通过
1 | pps.submit(dir) |
号
我得到和你一样的错误:
1
2
3
4
5
6
7 Traceback (most recent call last):
File"parallel.py", line 9, in
pps.submit(dir)
...
File".../lib/python2.7/inspect.py", line 420, in getfile
'function, traceback, frame, or code object'.format(object))
TypeError: is not a module, class, method, function, traceback, frame, or code object
号以下讨论后更新:
所以这里的问题是,Python将来自C扩展的成员视为内置的。上面的代码与常规的python模块一起工作,但是我能够在从C扩展导入和传递函数时复制op的错误。
因此,我将C扩展函数调用包装在一个普通的python函数中,这就完成了技巧。注意,现在
1 2 3 4 5 6 7 8 9 10 11 | import pp def walk(n): import walkerc return walkerc.walk(n) def print_callback(result): print('callback: ', result) pps = pp.Server() job = pps.submit(walk, args=(1,), callback=print_callback) |