[英]Terminating a multiprocessing process from main
I am using Redis Pubsub to trigger a child process and save a reference to it.我正在使用 Redis Pubsub 来触发子进程并保存对它的引用。 I would like to terminate the previous process when the next message comes in. Unfortunately, though I can see the child process in debugger, and it has a terminate() function, it does not seem like main can see it-I get an error saying 'NoneType' object has no attribute 'terminate'.我想在下一条消息进来时终止前一个进程。不幸的是,虽然我可以在调试器中看到子进程,并且它有一个 terminate() function,但似乎 main 看不到它 - 我收到一个错误说“NoneType”object 没有“终止”属性。 Is there a straightforward way to terminate the process?有没有一种直接的方法来终止这个过程?
My code (in ' main ':我的代码(在' main '中:
conn = redis.Redis(host="localhost", port="6379")
if not conn.ping():
raise Exception('Redis unavailable')
pubsub = conn.pubsub()
pubsub.subscribe("feed")
data = None
loaderProcess = None
for message in pubsub.listen():
logging.info("received pubsub message")
logging.info(message)
logging.info(message['type'])
if message['type'] == "message":
data = json.loads(message.get("data"))
if data and data['source']:
try:
if loaderProcess is not None:
loaderProcess.terminate()
loaderProcess.join()
args.infile = data['source']
loader = Video(infile=data.get("source"), fps=30.0)
loaderProcess = multiprocessing.Process(target=load, args = (loader, conn, args,))
except Exception as e:
logging.error("Error occurred", exc_info=True)
Stack trace:堆栈跟踪:
ERROR:root:Error occurred
Traceback (most recent call last):
File "C:\video-analysis\capture.py", line 140, in <module>
loaderProcess.terminate()
File "C:\Users\bkogan\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 133, in terminate
self._popen.terminate()
OK, got it.好的,我知道了。 A few issues.几个问题。 One was that I was not starting the process explicitly.一是我没有明确地开始这个过程。 Another is that my reference to the process was not working.另一个是我对这个过程的引用不起作用。 Adding the process to a list works, though:但是,将进程添加到列表中是可行的:
procs = []
for message in pubsub.listen():
try:
if message['type'] == "message":
data = json.loads(message.get("data"))
if data and data['source']:
for proc in procs:
if proc.is_alive():
proc.terminate()
proc.join(timeout=0)
procs.pop(0)
loaderProcess = multiprocessing.Process(target=load, args = (data.get("source"), args,))
procs.append(loaderProcess)
loaderProcess.start()
continue
except Exception as e:
logging.error("Error occurred", exc_info=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.