I would like to renew the question about multiprocessing inside decorator (my previous question seems to me dead :) ). I stumbled on this problem and unfortunately I have no idea how to solve this. For need of my application I have to use multiprocessing inside the decorator but... when I use multiprocessing inside decorator I get error:
Can't pickle <function run_testcase at 0x00000000027789C8>: it's not found as __main__.run_testcase
.
In other hand, when I call my multiprocessing function like normal function wrapper(function,*arg)
it works. This is very tricky, but I have no idea what am I doing wrong. I am close to conclude that this is python error :). Maybe someone knows the workaround to this problem leaving the same syntax. I run this code on Windows (unluckily).
The previous question: Using multiprocessing inside decorator generates error: can't pickle function...it's not found as
The simplest code to simulate this error:
from multiprocessing import Process,Event
class ExtProcess(Process):
def __init__(self, event,*args,**kwargs):
self.event=event
Process.__init__(self,*args,**kwargs)
def run(self):
Process.run(self)
self.event.set()
class PythonHelper(object):
@staticmethod
def run_in_parallel(*functions):
event=Event()
processes=dict()
for function in functions:
fname=function[0]
try:fargs=function[1]
except:fargs=list()
try:fproc=function[2]
except:fproc=1
for i in range(fproc):
process=ExtProcess(event,target=fname,args=fargs)
process.start()
processes[process.pid]=process
event.wait()
for process in processes.values():
process.terminate()
for process in processes.values():
process.join()
class Recorder(object):
def capture(self):
while True:print("recording")
from z_helper import PythonHelper
from z_recorder import Recorder
def wrapper(fname,*args):
try:
PythonHelper.run_in_parallel([fname,args],[Recorder().capture])
print("success")
except Exception as e:
print("failure: {}".format(e))
from z_wrapper import wrapper
from functools import wraps
class Report(object):
@staticmethod
def debug(fname):
@wraps(fname)
def function(*args):
wrapper(fname,args)
return function
executing:
from z_report import Report
import time
class Test(object):
@Report.debug
def print_x(self,x):
for index,data in enumerate(range(x)):
print(index,data); time.sleep(1)
if __name__=="__main__":
Test().print_x(10)
I added @wraps to the previous version
My Traceback:
Traceback (most recent call last):
File "C:\Interpreters\Python32\lib\pickle.py", line 679, in save_global
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'run_testcase'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\EskyTests\w_Logger.py", line 19, in <module>
logger.run_logger()
File "C:\EskyTests\w_Logger.py", line 14, in run_logger
self.run_testcase()
File "C:\EskyTests\w_Decorators.py", line 14, in wrapper
PythonHelper.run_in_parallel([function,args],[recorder.capture])
File "C:\EskyTests\w_PythonHelper.py", line 25, in run_in_parallel
process.start()
File "C:\Interpreters\Python32\lib\multiprocessing\process.py", line 130, in start
self._popen = Popen(self)
File "C:\Interpreters\Python32\lib\multiprocessing\forking.py", line 267, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Interpreters\Python32\lib\multiprocessing\forking.py", line 190, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:\Interpreters\Python32\lib\pickle.py", line 237, in dump
self.save(obj)
File "C:\Interpreters\Python32\lib\pickle.py", line 344, in save
self.save_reduce(obj=obj, *rv)
File "C:\Interpreters\Python32\lib\pickle.py", line 432, in save_reduce
save(state)
File "C:\Interpreters\Python32\lib\pickle.py", line 299, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Interpreters\Python32\lib\pickle.py", line 623, in save_dict
self._batch_setitems(obj.items())
File "C:\Interpreters\Python32\lib\pickle.py", line 656, in _batch_setitems
save(v)
File "C:\Interpreters\Python32\lib\pickle.py", line 299, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Interpreters\Python32\lib\pickle.py", line 683, in save_global
(obj, module, name))
_pickle.PicklingError: Can't pickle <function run_testcase at 0x00000000027725C8>: it's not found as __main__.run_testcase