Python 为什么我的并行代码会产生错误?
问题1:当Python 为什么我的并行代码会产生错误?,python,parallel-processing,Python,Parallel Processing,问题1:当sys.stdout.write未包装在单独的函数中时,下面的代码将失败 问题2:当ssys.stdout.write被包装在一个单独的函数中时,代码会打印每个字母之间的空格 代码(v1): 跟踪: $ ./parhello.py Traceback (most recent call last): File "./parhello.py", line 15, in <module> main() File "./parhello.py", line 12,
sys.stdout.write
未包装在单独的函数中时,下面的代码将失败
问题2:当ssys.stdout.write
被包装在一个单独的函数中时,代码会打印每个字母之间的空格
代码(v1):
跟踪:
$ ./parhello.py
Traceback (most recent call last):
File "./parhello.py", line 15, in <module>
main()
File "./parhello.py", line 12, in main
server.submit(write, (c,), (), ("sys",))()
File "/Library/Python/2.7/site-packages/pp.py", line 461, in submit
sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)
File "/Library/Python/2.7/site-packages/pp.py", line 639, in __dumpsfunc
sources = [self.__get_source(func) for func in funcs]
File "/Library/Python/2.7/site-packages/pp.py", line 706, in __get_source
sourcelines = inspect.getsourcelines(func)[0]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 688, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 527, in findsource
file = getsourcefile(object)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 446, in getsourcefile
filename = getfile(object)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 422, in getfile
'function, traceback, frame, or code object'.format(object))
TypeError: <built-in method write of file object at 0x1002811e0> is not a module, class, method, function, traceback, frame, or code object
make: *** [test] Error 1
$ ./parhello.py
H e l l o W o r l d !
跟踪:
$ ./parhello.py
Traceback (most recent call last):
File "./parhello.py", line 15, in <module>
main()
File "./parhello.py", line 12, in main
server.submit(write, (c,), (), ("sys",))()
File "/Library/Python/2.7/site-packages/pp.py", line 461, in submit
sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)
File "/Library/Python/2.7/site-packages/pp.py", line 639, in __dumpsfunc
sources = [self.__get_source(func) for func in funcs]
File "/Library/Python/2.7/site-packages/pp.py", line 706, in __get_source
sourcelines = inspect.getsourcelines(func)[0]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 688, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 527, in findsource
file = getsourcefile(object)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 446, in getsourcefile
filename = getfile(object)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 422, in getfile
'function, traceback, frame, or code object'.format(object))
TypeError: <built-in method write of file object at 0x1002811e0> is not a module, class, method, function, traceback, frame, or code object
make: *** [test] Error 1
$ ./parhello.py
H e l l o W o r l d !
对于第一部分,pp的设计不是为了将内置函数作为参数处理到
submit
。第二个问题更复杂。在pp调用提交的函数之前,它将stdout和stderr重定向到StringIO对象。完成任务后,它将使用
print sout,
这意味着在打印之前,它会将sout
的内容附加一个空格。为了避免这种情况,不要让函数直接使用sys.stdout,而是打印到您管理的文件或队列中,并以更好的方式处理打印