PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/pp-1.6.0/ppworker.py

#
Python | 123 lines | 69 code | 19 blank | 35 comment | 10 complexity | 31fde82bc028b2cec4cea7117a6bbcb1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # Parallel Python Software: http://www.parallelpython.com
  2. # Copyright (c) 2005-2010, 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.
  14. #
  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
  28. http://www.parallelpython.com - updates, documentation, examples and support
  29. forums
  30. """
  31. import sys
  32. import os
  33. import StringIO
  34. import cPickle as pickle
  35. import pptransport
  36. copyright = "Copyright (c) 2005-2010 Vitalii Vanovschi. All rights reserved"
  37. version = "1.6.0"
  38. def import_module(name):
  39. mod = __import__(name)
  40. components = name.split('.')
  41. for comp in components[1:]:
  42. mod = getattr(mod, comp)
  43. return mod
  44. def preprocess(msg):
  45. fname, fsources, imports = pickle.loads(msg)
  46. fobjs = [compile(fsource, '<string>', 'exec') for fsource in fsources]
  47. for module in imports:
  48. try:
  49. globals()[module.split('.')[0]] = import_module(module)
  50. except:
  51. print "An error has occured during the module import"
  52. sys.excepthook(*sys.exc_info())
  53. return fname, fobjs
  54. class _WorkerProcess(object):
  55. def __init__(self):
  56. self.hashmap = {}
  57. self.e = sys.__stderr__
  58. self.sout = StringIO.StringIO()
  59. # self.sout = open("/tmp/pp.debug","a+")
  60. sys.stdout = self.sout
  61. sys.stderr = self.sout
  62. self.t = pptransport.CPipeTransport(sys.stdin, sys.__stdout__)
  63. self.t.send(str(os.getpid()))
  64. self.pickle_proto = int(self.t.receive())
  65. def run(self):
  66. try:
  67. #execution cycle
  68. while 1:
  69. __fname, __fobjs = self.t.creceive(preprocess)
  70. __sargs = self.t.receive()
  71. for __fobj in __fobjs:
  72. try:
  73. exec __fobj
  74. globals().update(locals())
  75. except:
  76. print "An error has occured during the " + \
  77. "function import"
  78. sys.excepthook(*sys.exc_info())
  79. __args = pickle.loads(__sargs)
  80. __f = locals()[__fname]
  81. try:
  82. __result = __f(*__args)
  83. except:
  84. print "An error has occured during the function execution"
  85. sys.excepthook(*sys.exc_info())
  86. __result = None
  87. __sresult = pickle.dumps((__result, self.sout.getvalue()),
  88. self.pickle_proto)
  89. self.t.send(__sresult)
  90. self.sout.truncate(0)
  91. except:
  92. print "A fatal error has occured during the function execution"
  93. sys.excepthook(*sys.exc_info())
  94. __result = None
  95. __sresult = pickle.dumps((__result, self.sout.getvalue()),
  96. self.pickle_proto)
  97. self.t.send(__sresult)
  98. if __name__ == "__main__":
  99. # add the directory with ppworker.py to the path
  100. sys.path.append(os.path.dirname(__file__))
  101. wp = _WorkerProcess()
  102. wp.run()
  103. # Parallel Python Software: http://www.parallelpython.com