Multithreading 使用多处理.Pipe的死锁
我正在尝试创建一个与线程通信的进程。 我试图向进程发送数据,进程需要对数据做一些处理,然后返回答案 我不明白是什么问题Multithreading 使用多处理.Pipe的死锁,multithreading,python-2.7,process,multiprocessing,pipe,Multithreading,Python 2.7,Process,Multiprocessing,Pipe,我正在尝试创建一个与线程通信的进程。 我试图向进程发送数据,进程需要对数据做一些处理,然后返回答案 我不明白是什么问题 class Worker(threading.Thread): """Thread executing tasks from a given tasks queue""" def __init__(self, tasks): threading.Thread.__init__(self) self.daemon = True
class Worker(threading.Thread):
"""Thread executing tasks from a given tasks queue"""
def __init__(self, tasks):
threading.Thread.__init__(self)
self.daemon = True
self.name = "RemoteWorker_%s" % id(self)
# Queue shared with all threads
self.tasks = tasks
# Pipe's shared with the current thread(self) and the process
self.procInput, self.threadOutput = Pipe(duplex=True)
self.threadInput, self.procOutput = Pipe(duplex=True)
# Create Process for running the Tasks.
self.proc = Process(target=execute,
args=(self.procOutput,
self.procInput))
self.proc.start()
self.start()
def run(self):
while True:
(func, args, kargs), _ = self.tasks.get()
func(self, *args, **kargs) # parent_proc_runner
def put(self, arg):
self.threadOutput.send(arg)
def get(self):
return self.threadInput.recv()
def stop(self):
self.tasks.put((StopIteration, None, None)) # stop thread
ProcessTask.KillProcess(self.proc.pid) # kill process
def execute(q):
procInput, procOutput = q
rc = None
exception = None
while True:
args, kwargs = procInput.recv()
rc, exception = runner(*args, **kwargs)
procOutput.send((rc, exception))
def parent_proc_runner(worker, *args, **kwargs):
rc = None
exception = None
worker.put((args, kwargs))
rc, exception = worker.get() # here i'm in a deadlock
return rc, exception
与我在队列中使用的代码相同,它工作正常,但我遇到了性能问题。
如果有人能帮助我,我将不胜感激
谢谢