本文介绍了为什么我的并行代码会产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:如果sys.stdout.write没有包装在单独的函数中,则下面的代码将失败.

Issue 1: When sys.stdout.write is not wrapped in a separate function, the code below fails.

问题2:将ssys.stdout.write包装在单独的函数中时,代码在每个字母之间打印空格.

Issue 2: When ssys.stdout.write is wrapped in a separate function, the code prints spaces between each letter.

代码(v1):

#!/usr/bin/env python

import pp
import sys

def main():
    server = pp.Server()

    for c in "Hello World!\n":
        server.submit(sys.stdout.write, (c,), (), ("sys",))()

if __name__=="__main__":
    main()

踪迹:

$ ./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

代码(v2):

#!/usr/bin/env python

import pp
import sys

def hello(c):
    sys.stdout.write(c)

def main():
    server = pp.Server()

    for c in "Hello World!\n":
        server.submit(hello, (c,), (), ("sys",))()

if __name__=="__main__":
    main()

踪迹:

$ ./parhello.py
H e l l o   W o r l d !

推荐答案

在第一部分中,pp并非旨在将内置函数作为submit的参数来处理.第二个问题更加复杂.在pp调用提交的函数之前,它将stdout和stderr重定向到StringIO对象.完成任务后,它会使用

For the first part, pp wasn't designed to handle built-ins as arguments to submit. The second problem is more complicated. Before pp calls the submitted function, it redirects stdout and stderr to a StringIO object. On completing the task, it prints the value from the StringIO object with

print sout,

这意味着它将在打印sout的内容之前添加一个空格.要解决此问题,不要让您的函数直接使用sys.stdout,而是将其打印到您管理的文件或队列中,并以更好的方式处理打印.

This means that it appends a space the contents of sout before printing it. To get around this, don't have your functions use sys.stdout directly, but print either to a file or a queue you manage and handle the printing of in a better way.

这篇关于为什么我的并行代码会产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 03:06