add gitignore
[pp3.git] / ppworker.py
blobef6cdb5eb47e1d61386216bc7f9d2211bcb2d6d4
1 # Parallel Python Software: http://www.parallelpython.com
2 # Copyright (c) 2005-2009, Vitalii Vanovschi
3 # All rights reserved.
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.
26 """
27 Parallel Python Software, PP Worker
29 http://www.parallelpython.com - updates, documentation, examples and support
30 forums
31 """
32 import sys
33 import os
34 import StringIO
35 import cPickle as pickle
36 import pptransport
38 copyright = "Copyright (c) 2005-2009 Vitalii Vanovschi. All rights reserved"
39 version = "1.5.7"
42 def import_module(name):
43 mod = __import__(name)
44 components = name.split('.')
45 for comp in components[1:]:
46 mod = getattr(mod, comp)
47 return mod
50 def preprocess(msg):
51 fname, fsources, imports = pickle.loads(msg)
52 fobjs = [compile(fsource, '<string>', 'exec') for fsource in fsources]
53 for module in imports:
54 try:
55 globals()[module.split('.')[0]] = __import__(module)
56 except:
57 print "An error has occured during the module import"
58 sys.excepthook(*sys.exc_info())
59 return fname, fobjs
62 class _WorkerProcess(object):
64 def __init__(self):
65 self.hashmap = {}
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())
75 def run(self):
76 try:
77 #execution cycle
78 while 1:
80 __fname, __fobjs = self.t.creceive(preprocess)
82 __sargs = self.t.receive()
84 for __fobj in __fobjs:
85 try:
86 eval(__fobj)
87 globals().update(locals())
88 except:
89 print "An error has occured during the " + \
90 "function import"
91 sys.excepthook(*sys.exc_info())
93 __args = pickle.loads(__sargs)
95 __f = locals()[__fname]
96 try:
97 __result = __f(*__args)
98 except:
99 print "An error has occured during the function execution"
100 sys.excepthook(*sys.exc_info())
101 __result = None
103 __sresult = pickle.dumps((__result, self.sout.getvalue()),
104 self.pickle_proto)
106 self.t.send(__sresult)
107 self.sout.truncate(0)
108 except:
109 print "Fatal error has occured during the function execution"
110 sys.excepthook(*sys.exc_info())
111 __result = None
112 __sresult = pickle.dumps((__result, self.sout.getvalue()),
113 self.pickle_proto)
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()
121 wp.run()
123 # Parallel Python Software: http://www.parallelpython.com