Why does my parallel code generate an error?

Problem 1: When sys.stdout.write not wrapped in a separate function, the code below does not work.

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

Code (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() 

Trace:

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

Code (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() 

Trace:

 $ ./parhello.py H ello W orld ! 
+4
source share
1 answer

In the first part, pp was not intended to handle built-in functions as arguments to submit . The second problem is more complicated. Before pp calls the function presented, it redirects stdout and stderr to a StringIO object. Upon completion of the task, it prints the value from the StringIO object using

 print sout, 

This means that before printing, it adds the contents of sout . To get around this, do not use your sys.stdout functions directly, but print either to a file or to a queue that you manage, and better handle printing.

0
source

All Articles