多重处理,Python3,Windows:TypeError:无法腌制_thread.lock对象

时间:2018-09-14 17:03:54

标签: python python-3.x redis celery python-multiprocessing

在运行Python 3.6的Windows 10系统上,尝试使用multiprocessing.Process创建新的rq工作线程时,

multiprocessing.Process(target=Worker(qs).work, kwargs={'burst': True}).start()     

我们遇到错误

  

TypeError:无法腌制_thread.lock对象

  

OSError:[WinError 87]参数不正确

使用

rq.SimpleWorker代替rq.Worker,因为Windows不支持fork使用的rq.Worker功能。

问题:是什么导致此错误?我们该如何解决?

from rq import SimpleWorker, Connection

def needMoreWorkers():
    return True

if __name__ == '__main__':
    qs = sys.argv[1:] or ['default']
    with Connection(connection=my_redis_conn):
        if needMoreWorkers():
            multiprocessing.Process(target=SimpleWorker(qs).work, kwargs={'burst': True}).start()     

错误回溯

Traceback (most recent call last):
File "WorkerFactory.py", line 53, in <module>
    main(qs)
File "WorkerFactory.py", line 45, in main
    multiprocessing.Process(target=SimpleWorker(qs)).start()
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects

(test) C:\Code\test\source>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\spawn.py", line 99, in spawn_main
    new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File "C:\Users\x\AppData\Local\Continuum\anaconda3\envs\test\lib\multiprocessing\reduction.py", line 82, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect   

1 个答案:

答案 0 :(得分:0)

据我所知,Pickle模块导致了该问题。必须将其导入另一个模块中的某个位置。无论如何,pickle用于保存数据,但显然不能“刺穿” thread.lock对象。至于如何解决它,请检查所有函数调用,并确保传递正确的变量类型。