1 # Parallel Python Software: http://www.parallelpython.com
2 # Copyright (c) 2005-2009, Vitalii Vanovschi
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 # * Redistributions of source code must retain the above copyright notice,
7 # this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
11 # * Neither the name of the author nor the names of its contributors
12 # may be used to endorse or promote products derived from this software
13 # without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 # THE POSSIBILITY OF SUCH DAMAGE.
27 Parallel Python Software, PP Worker
29 http://www.parallelpython.com - updates, documentation, examples and support
35 import cPickle
as pickle
38 copyright
= "Copyright (c) 2005-2009 Vitalii Vanovschi. All rights reserved"
42 def import_module(name
):
43 mod
= __import__(name
)
44 components
= name
.split('.')
45 for comp
in components
[1:]:
46 mod
= getattr(mod
, comp
)
51 fname
, fsources
, imports
= pickle
.loads(msg
)
52 fobjs
= [compile(fsource
, '<string>', 'exec') for fsource
in fsources
]
53 for module
in imports
:
55 globals()[module
.split('.')[0]] = __import__(module
)
57 print "An error has occured during the module import"
58 sys
.excepthook(*sys
.exc_info())
62 class _WorkerProcess(object):
66 self
.e
= sys
.__stderr
__
67 self
.sout
= StringIO
.StringIO()
68 # self.sout = open("/tmp/pp.debug","a+")
69 sys
.stdout
= self
.sout
70 sys
.stderr
= self
.sout
71 self
.t
= pptransport
.CPipeTransport(sys
.stdin
, sys
.__stdout
__)
72 self
.t
.send(str(os
.getpid()))
73 self
.pickle_proto
= int(self
.t
.receive())
80 __fname
, __fobjs
= self
.t
.creceive(preprocess
)
82 __sargs
= self
.t
.receive()
84 for __fobj
in __fobjs
:
87 globals().update(locals())
89 print "An error has occured during the " + \
91 sys
.excepthook(*sys
.exc_info())
93 __args
= pickle
.loads(__sargs
)
95 __f
= locals()[__fname
]
97 __result
= __f(*__args
)
99 print "An error has occured during the function execution"
100 sys
.excepthook(*sys
.exc_info())
103 __sresult
= pickle
.dumps((__result
, self
.sout
.getvalue()),
106 self
.t
.send(__sresult
)
107 self
.sout
.truncate(0)
109 print "Fatal error has occured during the function execution"
110 sys
.excepthook(*sys
.exc_info())
112 __sresult
= pickle
.dumps((__result
, self
.sout
.getvalue()),
114 self
.t
.send(__sresult
)
117 if __name__
== "__main__":
118 # add the directory with ppworker.py to the path
119 sys
.path
.append(os
.path
.dirname(__file__
))
120 wp
= _WorkerProcess()
123 # Parallel Python Software: http://www.parallelpython.com