Python 从主服务器终止多处理进程
我正在使用Redis Pubsub触发一个子进程并保存对它的引用。我想在收到下一条消息时终止上一个进程。不幸的是,虽然我可以在调试器中看到子进程,并且它有一个terminate()函数,但似乎main无法看到它——我得到一个错误,说“NoneType”对象没有属性“terminate”。是否有一种直接的方法来终止该过程 我的代码(在“main”中):Python 从主服务器终止多处理进程,python,python-multiprocessing,Python,Python Multiprocessing,我正在使用Redis Pubsub触发一个子进程并保存对它的引用。我想在收到下一条消息时终止上一个进程。不幸的是,虽然我可以在调试器中看到子进程,并且它有一个terminate()函数,但似乎main无法看到它——我得到一个错误,说“NoneType”对象没有属性“terminate”。是否有一种直接的方法来终止该过程 我的代码(在“main”中): conn = redis.Redis(host="localhost", port="6379") if
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()
错误:根:发生错误
回溯(最近一次呼叫最后一次):
文件“C:\video analysis\capture.py”,第140行,在
loaderProcess.terminate()
文件“C:\Users\bkogan\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py”,第133行,终止
self.\u 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)
cmd-“killall-9 process”用于所有正在运行的进程如果您有任何进程id,那么您可以使用-cmd-kill“processid”您可以添加完整的堆栈跟踪吗?终止并加入应该是关闭进程的正确方法是,添加了堆栈跟踪。