typo error
[pp3.git] / ppworker.py
blob9171ece1df7784a02b78976f79fa879ed95c0fde
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
5 # All rights reserved.
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.
29 import sys
30 import os
31 import io
32 import pickle as pickle
33 import pptransport
35 copyright = "Copyright (c) 2009 Ondřej Súkup /n \
36 Copyright (c) 2005-2009 Vitalii Vanovschi. All rights reserved"
37 version = "3.0.0"
39 def import_module(name):
40 mod = __import__(name)
41 components = name.split('.')
42 for comp in components[1:]:
43 mod = getattr(mod, comp)
44 return mod
47 def preprocess(msg):
48 fname, fsources, imports = pickle.loads(msg)
49 fobjs = [compile(fsource, '<string>', 'exec') for fsource in fsources]
50 for module in imports:
51 try:
52 globals()[module.split('.')[0]] = __import__(module)
53 except:
54 print("An error has occured during the module import")
55 sys.excepthook(*sys.exc_info())
56 return fname, fobjs
59 class _WorkerProcess(object):
61 def __init__(self):
62 self.hashmap = {}
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())
72 def run(self):
73 try:
74 #execution cycle
75 while 1:
77 __fname, __fobjs = self.t.creceive(preprocess)
79 __sargs = self.t.receive()
81 for __fobj in __fobjs:
82 try:
83 eval(__fobj)
84 globals().update(locals())
85 except:
86 print("An error has occured during the " + \
87 "function import")
88 sys.excepthook(*sys.exc_info())
90 __args = pickle.loads(__sargs)
92 __f = locals()[__fname]
93 try:
94 __result = __f(*__args)
95 except:
96 print("An error has occured during the function execution")
97 sys.excepthook(*sys.exc_info())
98 __result = None
100 __sresult = pickle.dumps((__result, self.sout.getvalue()),
101 self.pickle_proto)
103 self.t.send(__sresult)
104 self.sout.truncate(0)
105 except:
106 print("Fatal error has occured during the function execution")
107 sys.excepthook(*sys.exc_info())
108 __result = None
109 __sresult = pickle.dumps((__result, self.sout.getvalue()),
110 self.pickle_proto)
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()
118 wp.run()
120 # Parallel Python Software: http://www.parallelpython.com