spacepaste

Differences between the pastes #21853 (May 15, 2012 1:31:43 AM) and #21856 (May 15, 2012 1:32:31 AM). Download as unified diff.

1 from Queue import Queue
2 from multiprocessing import Process
3
4 def returning_wrapper(func, *args, **kwargs):
5 kwargs.get("multiprocess_returnable").put(func(*args, **kwargs))
6
7 class Multiprocess(object):
8 """Cute decorator to run a function in multiple processes."""
9 def __init__(self, func):
10 self.func = func
11 self.processes = []
12
13 def __call__(self, *args, **kwargs):
14 num_processes = kwargs.get("multiprocess_num_processes", 2) # default to two processes.
15 return_obj = kwargs.get("multiprocess_returnable", Queue()) # default to stdlib Queue
16 for i in xrange(num_processes):
17 pro = Process(target=returning_wrapper, args=tuple([self.func] + list(args)), kwargs=kwargs)
18 self.processes.append(pro)
19 pro.start()
20
21 def __eq__(self, other):
22 # Fuck you pickle
23 return True
24
25 @Multiprocess
26 def info():
27 print title
28 print 'module name:', __name__
29 print 'parent process:', os.getppid()
30 print 'process id:', os.getpid()
31
32 info()
1 Traceback (most recent call last):
2 File "C:/languages/Python27/test.py", line 32, in <module>
3 info()
4 File "C:/languages/Python27/test.py", line 19, in __call__
5 pro.start()
6 File "C:\languages\Python27\lib\multiprocessing\process.py", line 130, in start
7 self._popen = Popen(self)
8 File "C:\languages\Python27\lib\multiprocessing\forking.py", line 271, in __init__
9 dump(process_obj, to_child, HIGHEST_PROTOCOL)
10 File "C:\languages\Python27\lib\multiprocessing\forking.py", line 193, in dump
11 ForkingPickler(file, protocol).dump(obj)
12 File "C:\languages\Python27\lib\pickle.py", line 224, in dump
13 self.save(obj)
14 File "C:\languages\Python27\lib\pickle.py", line 331, in save
15 self.save_reduce(obj=obj, *rv)
16 File "C:\languages\Python27\lib\pickle.py", line 419, in save_reduce
17 save(state)
18 File "C:\languages\Python27\lib\pickle.py", line 286, in save
19 f(self, obj) # Call unbound method with explicit self
20 File "C:\languages\Python27\lib\pickle.py", line 649, in save_dict
21 self._batch_setitems(obj.iteritems())
22 File "C:\languages\Python27\lib\pickle.py", line 681, in _batch_setitems
23 save(v)
24 File "C:\languages\Python27\lib\pickle.py", line 286, in save
25 f(self, obj) # Call unbound method with explicit self
26 File "C:\languages\Python27\lib\pickle.py", line 548, in save_tuple
27 save(element)
28 File "C:\languages\Python27\lib\pickle.py", line 286, in save
29 f(self, obj) # Call unbound method with explicit self
30 File "C:\languages\Python27\lib\pickle.py", line 753, in save_global
31 (obj, module, name))
32 PicklingError: Can't pickle <function info at 0x0000000002F18828>: it's not the same object as __main__.info