26 This decorator implements the forking patterns, i.e. it runs the function
29 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/511474
31 @functools.wraps(func)
32 def wrapper(*args, **kwargs):
37 pread, pwrite = os.pipe()
45 with os.fdopen(pread,
'rb')
as f:
46 status, result = pickle.load(f)
51 remote_exc = result[0]
58 result = func(*args, **kwargs)
60 except (Exception, KeyboardInterrupt)
as exc:
62 exc_string = traceback.format_exc(limit=10)
63 for l
in exc_string.splitlines():
64 print (
"[%d]"%os.getpid(),l.rstrip())
66 result = exc, exc_string
68 with os.fdopen(pwrite,
'wb')
as f:
70 pickle.dump((status,result), f, pickle.HIGHEST_PROTOCOL)
71 except pickle.PicklingError
as exc:
72 pickle.dump((2,exc), f, pickle.HIGHEST_PROTOCOL)