我正在尝试创建一个在单独的线程中运行装饰函数的装饰器。我将这些装饰器放在一个额外的模块中,以便能够在任何我想要的地方使用它们。
模块" runSeparate":
import multiprocessing
import threading
import functools
def runInSeparateThread(func):
@functools.wraps(func)
def newFun(*args, **kwargs):
extraThread = threading.Thread(target=func, args=args, kwargs=kwargs)
extraThread.start()
return extraThread
return newFun
def runInSeparateProcess(func):
@functools.wraps(func)
def newFun(*args, **kwargs):
extraProcess = multiprocessing.Process(target=func, args=args, kwargs=kwargs)
extraProcess.start()
return extraProcess
return newFun
请注意,两个装饰器功能几乎完全相同。一个使用threading.Thread和另一个使用multiprocessing.Process。
在我的main函数中,我调用另一个函数并用上面的装饰器装饰它。
@runSeparate.runInSeparateThread
def test2():
someOtherModule.test() # contains only one print command
if __name__ == "__main__":
t = test2()
这很好,但是一旦我将装饰器更改为
@runSeparate.runInSeparateProcess
我得到一个酸洗错误:
C:\Programme\Python27\python.exe "D:/MKS Integrity/TU/Entwicklung/ControlDeskSkripte/LayoutSkripte/flashLayout.py"
Traceback (most recent call last):
File "D:/MKS Integrity/TU/Entwicklung/ControlDeskSkripte/LayoutSkripte/flashLayout.py", line 72, in <module>
t = test()
File "D:\MKS Integrity\TU\Entwicklung\ControlDeskSkripte\HiLSkripte\runSeparate.py", line 27, in newFun
extraProcess.start()
File "C:\Programme\Python27\lib\multiprocessing\process.py", line 130, in start
self._popen = Popen(self)
File "C:\Programme\Python27\lib\multiprocessing\forking.py", line 277, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Programme\Python27\lib\multiprocessing\forking.py", line 199, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:\Programme\Python27\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Programme\Python27\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Programme\Python27\lib\pickle.py", line 419, in save_reduce
save(state)
File "C:\Programme\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Programme\Python27\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Programme\Python27\lib\pickle.py", line 681, in _batch_setitems
save(v)
File "C:\Programme\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Programme\Python27\lib\pickle.py", line 753, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <function test at 0x022AD330>: it's not the same object as __main__.test
我想使用多处理模块,并找到许多解决类似主题的问题。某种程度上,酸洗模块在酸洗和去除功能方面并不出色,多处理模块使用它。在这个(http://code.activestate.com/recipes/576684-simple-threading-decorator/)页面上有一个创建线程装饰器的例子。 Tetsuya Morimoto下面的评论说他设法将它改为多处理装饰器,但是使用上面提供的例子和他的改变我得到了同样的错误。它没有说明他们使用的是什么版本的Python。
我在Windows 7上使用Python 2.7.8。有什么方法可以解决这个问题吗?我错过了多处理和线程之间的一些区别吗? 提前谢谢!