I came across a problem that I can't solve and it's associated with multiprocessing and use it inside the decorator.
When I'm calling the method run_in_parallels using multiprocessing I 'm getting the error: - Can't pickle <function run_testcase at 0x00000000027789C8>: it's not found as __main__.run_testcase
The call takes place inside the decorator, then followed the above-mentioned problem. At the time of calling the same method run_in_parallels without a decorator all working properly.
What is the reason of this problem? file: w_PythonHelper.py desc: Function 'run_in_parallel' is used to run multiple processes simultaneously. The first method, which will end operation stops the others. -
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()
-
file: w_Recorder.py desc: function 'capture' is used to grab a screenshot -
from PIL import ImageGrab
-
-
def capture(self,filename=time.time(),extension="png"):
-
ImageGrab.grab().save("{f}.{e}".format(f=filename,e=extension))
-
file: w_Decorators.py desc: Running parallel a given function along with a method 'capture' of class 'Recorder' -
from w_Recorder import Recorder
-
from w_PythonHelper import PythonHelper
-
-
def check(function):
-
def wrapper(*args):
-
try:
-
recorder=Recorder()
-
PythonHelper.run_in_parallel([function,args],[recorder.capture])
-
print("success")
-
except Exception as e:
-
print("failure: {}".format(e))
-
return function
-
return wrapper
-
file: w_Logger.py desc: Main program (generates error) -
from w_Decorators import check
-
import time
-
-
class Logger(object):
-
-
@check
-
def run_testcase(self):
-
# example function (runtime: 20s)
-
for i in range(20):
-
print("number: {}".format(i))
-
time.sleep(1)
-
-
def run_logger(self):
-
self.run_testcase()
-
-
-
if __name__=="__main__":
-
logger=Logger()
-
logger.run_logger()
-
file: w_Logger.py desc: Main program (works corectly) -
from w_PythonHelper import PythonHelper
-
from w_Recorder import Recorder
-
import time
-
-
class Logger(object):
-
-
def run_testcase(self):
-
# example function (runtime: 20s)
-
for i in range(20):
-
print("number: {}".format(i))
-
time.sleep(1)
-
-
def run_logger(self):
-
recorder=Recorder()
-
PythonHelper.run_in_parallel([self.run_testcase],[recorder.capture])
-
-
if __name__=="__main__":
-
logger=Logger()
-
logger.run_logger()
-
What is the difference that these same methods presented in the two cases work differently?
I got only one answer on this strange question: -
import traceback
-
def wrapper(function,*args):
-
try:
-
recorder=Recorder()
-
PythonHelper().run_in_parallel([function,args],[recorder.capture])
-
print("success")
-
except Exception,e:
-
print("failure: "+traceback.format_exc(10))
-
from w_Decorators import wrapper -
if __name__=="__main__":
-
logger=Logger()
-
wrapper(logger.run_testcase)
-
I can use wrapper as a function, but can't use it as decorator. I have many functions decorated by this decorator and the simpliest way is to use multiprocessing inside decorator. But unfortunatly I can't solve this problem. Maybe someone has already solved a similar problem. I would be grateful for any hint.
I've trying to make run_testcase a top level function but still nothing.
2 3988
"pickle errors" generally means that multiprocessing can not pickle some method (you pass as class method as the target), which it uses to pass things around. The problem here is not the pickle error. The problem here is code that isn't tested as you go along. Start with smaller pieces and test each piece as you go along as you would then know that it is a multiprocessing problem and not a decorator problem, at least. And read the multiprocessing docs-->hint: you want to pass a function that is part of the class which will do the processing. Also this code - def capture(self,filename=time.time(),extension="png"):
-
ImageGrab.grab().save("{f}.{e}".format(f=filename,e=extensiom))
will use the same file every time since time.time() will not change. I will leave it up to you to discover why as you should acquire some more knowledge about Python before you attempt a project like this.
Yes. You are right about the timestamp (this is only example and doesn't affect the issue) but decorator works ok - decorator is working properly, is used for a long time and takes a lot of methods (including static methods). The problem occurred when using multiprocessing. 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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dSchwartz |
last post by:
What I think I'm looking for is a way (in XSL stylesheet) to get the
contents of a directory. How can this be accomplished?
more detailed:
/root
index.aspx
xsl_style.xsl
/xml...
|
by: Daniel |
last post by:
Hello =)
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -
self.setInterval('ObjectName.methodName()',...
|
by: Tatu Portin |
last post by:
I have a class:
class Complex {
public:
double x, y;
Complex () {x = 0.0; y = 0.0;};
Complex operator * (Complex);
|
by: Hasani \(remove nospam from address\) |
last post by:
I have a website with 2 aspx pages
Foo.aspx, and bar.aspx
The content of both files
is
//in 1 file
Hello <%=Session.ToString()%>
================
|
by: Joel Byrd |
last post by:
I've been using an eval() statement, which has been working fine until
I put it inside of a function. Is this a known problem, and is there a
known solution/work-around to this?
|
by: Max Evans |
last post by:
I have a XML file, which contains itemid-elements, e.g.:
<itemid>3</itemid>
<itemid>12</itemid>
Now I want to convert these IDs to the corresponding name via XSLT. I
thought I could do it this...
|
by: Nathan Sokalski |
last post by:
I have a class that is just a bunch of Shared functions (basically, a bunch
of utilities I have grouped together). However, there are functions that I
want to write to use inside some of these...
|
by: ertis6 |
last post by:
Hi all,
I need to calculate a value inside 8 nested for loops. 2 additional
for loops are used during calculation. It was working fine with 4
loops. My code is like this:
...
for(int i1=0;...
|
by: nhwarriors |
last post by:
I am attempting to use the (new in 2.6) multiprocessing package to
process 2 items in a large queue of items simultaneously. I'd like to
be able to print to the screen the results of each item...
|
by: sgurukrupagmailcom |
last post by:
Hi,
I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here:
class Exc
{
Exc ()
{
System.out.println ("Haribol");
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |