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