Python - 在 Process 子类中创建 Thread 子类的问题 - 线程和多处理



我在试图让线程(threading.Thread)子类在进程(multiprocessing.Process)子类中工作时遇到了麻烦。

下面是演示这个问题的最简单的工作示例。我做了一个"子过程"(multiprocessing.Process的实例),它将包含一个子"WorkerThread"(线程的实例。执行终止于subProcess.start()

import multiprocessing
import threading
class SubProcess(multiprocessing.Process):
def __init__(self):
multiprocessing.Process.__init__(self, daemon=True)
#Instantiate this one worker
self.workerThread = WorkerThread()
def run(self):
#Start the worker
self.workerThread.start()
class WorkerThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self, daemon=True)
def run(self):
#Real work code goes here
pass

if __name__ == '__main__':
#Program starts here

#Instantiate the SubProcess class - works fine
subProcess = SubProcess()
#Start the subProcess - Execution stops here - see Traceback below
subProcess.start()
subProcess.join()

下面是输出回溯:

Traceback (most recent call last):
File "[***]simplestExampleError.py", line 31, in <module>
subProcess.start()
File "C:Python39libmultiprocessingprocess.py", line 121, in start
self._popen = self._Popen(self)
File "C:Python39libmultiprocessingcontext.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:Python39libmultiprocessingcontext.py", line 327, in _Popen
return Popen(process_obj)
File "C:Python39libmultiprocessingpopen_spawn_win32.py", line 93, in __init__
reduction.dump(process_obj, to_child)
File "C:Python39libmultiprocessingreduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_thread.lock' object
[***]>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Python39libmultiprocessingspawn.py", line 107, in spawn_main
new_handle = reduction.duplicate(pipe_handle,
File "C:Python39libmultiprocessingreduction.py", line 79, in duplicate
return _winapi.DuplicateHandle(
PermissionError: [WinError 5] Access is denied

我已经回顾了这个类似的问题,但关键的区别在于它们定义的是"workerthread"。指向一个带有预定义参数的函数(不是'threading.Thread'的子类)。示例:在子流程中,他们定义了workerThread = Thread(target=print_all_the_things, args=("a", self.num))

我需要能够在运行时更改WorkerThread的类变量。
示例:在subProcess中,当两者都运行

时,我会执行self.workerThread.input = "INPUT"

之类的操作任何帮助都将非常感激!

我看不出你发布的代码有任何理由(当然,你只发布了非常少的代码)为什么工作线程的实例化需要在SubProcess.__init__方法中完成。我只需要在run方法中初始化workerThread属性:

class SubProcess(multiprocessing.Process):
def __init__(self):
multiprocessing.Process.__init__(self, daemon=True)
def run(self):
#Instantiate this one worker
self.workerThread = WorkerThread()
#Start the worker
self.workerThread.start()

我不认为这是一个致命的缺陷,从ProcessThread类派生类,但它不是很灵活。如果使用SubProcess类的代码想要为进程分配一个name属性,该怎么办?在您当前的实现中,如果不重新设计__init__方法,它就无法实现。下面的实现更简洁,使类在非多处理、非多线程场景中可重用(对于这种情况,我可能会选择不同的方法名,而不是run,以更好地描述方法所完成的处理):

import multiprocessing
import threading
class SubProcess:
def __init__(self):
# set whatever attributes are required (that can be pickled)
...
def run(self): # or a more descriptive method name
#Instantiate this one worker
worker = Worker()
self.workerThread = threading.Thread(target=worker.run, daemon=True)
#Start the worker
self.workerThread.start()
class Worker:
def __init__(self):
# set whatever attributes are required
...
def run(self): # or a more descriptive method name
#Real work code goes here
pass

if __name__ == '__main__':
subprocess = SubProcess()
# In this context, the Process does not need to be a daemon process
# since we are waiting for its complettion:
p = multiprocessing.Process(target=subprocess.run, name='SubProcess')
p.start()
p.join()

相关内容

  • 没有找到相关文章

最新更新