admin管理员组文章数量:1123059
First of all, I am very new to multiprocessing. I am trying to implement a simple camera simulator that would generate images in a subprocess and put them in a queue for another subprocess to process them. An initialized camera simulator subprocess (camera) hangs/freezes, when I try to invoke the camera.join() method to finalize the program.
import multiprocessing
import queue
import time
import numpy as np
class CameraSimulator(multiprocessing.Process):
def __init__(self, queue):
super().__init__()
self.queue = queue
self.connected = multiprocessing.Event()
self.acquiring = multiprocessing.Event()
def run(self):
while self.connected.is_set():
while self.acquiring.is_set():
image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
try:
self.queue.put(image)
except queue.Full:
continue
time.sleep(0.1)
def connect(self):
self.connected.set()
self.start()
def disconnect(self):
self.connected.clear()
def acquire(self):
self.acquiring.set()
def stop(self):
self.acquiring.clear()
if __name__ == "__main__":
image_queue = multiprocessing.Queue(maxsize=1000)
camera = CameraSimulator(image_queue)
print("Connect camera")
camera.connect()
print("Start camera acquisition")
camera.acquire()
time.sleep(2)
print("Stop camera acquisition")
camera.stop()
time.sleep(2)
print("Start camera acquisition again")
camera.acquire()
time.sleep(2)
print("Stop camera acquisition")
camera.stop()
print("Disconnect camera")
camera.disconnect()
print("Draining queue")
while not image_queue.empty():
try:
image_queue.get_nowait()
except queue.Empty:
break
print("Queue drained")
camera.join()
print("Camera process terminated")
It seems that the problem might be that the queue is not empty. That's why I deliberately tried to drain the queue, but the problem still persists. Could it be that calling empty() is not reliable way to check, if a queue is empty due to multithreading/multiprocessing semantics? I could use camera.terminate() to forcefully terminate the subprocess, but that I would assume is not a good practice. Any help would be greatly appreciated!
本文标签: multiprocessingSubprocess in Python fails to terminate when Queue is utilizedStack Overflow
版权声明:本文标题:multiprocessing - Subprocess in Python fails to terminate when Queue is utilized - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736544172a1944424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论