繁体   English   中英

从 main 终止多处理进程

[英]Terminating a multiprocessing process from main

我正在使用 Redis Pubsub 来触发子进程并保存对它的引用。 我想在下一条消息进来时终止前一个进程。不幸的是,虽然我可以在调试器中看到子进程,并且它有一个 terminate() function,但似乎 main 看不到它 - 我收到一个错误说“NoneType”object 没有“终止”属性。 有没有一种直接的方法来终止这个过程?

我的代码(在' 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)

堆栈跟踪:

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()

好的,我知道了。 几个问题。 一是我没有明确地开始这个过程。 另一个是我对这个过程的引用不起作用。 但是,将进程添加到列表中是可行的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM