简体   繁体   English

Python 池 - 当我遇到超时异常时,如何保持 Python 进程运行?

[英]Python Pool - How do I keep Python Process running when I get a timeout exception?

I am using the Python multiprocessing library.我正在使用 Python 多处理库。 Whenever one of the processes throw a timeout error, my application ends itself.每当其中一个进程抛出超时错误时,我的应用程序就会自行结束。 I want to keep the processes up.我想保持流程正常进行。

I have a function that subscribes to a queue and listens to incoming messages:我有一个订阅队列并监听传入消息的 function:

def process_msg(i):
   #get new message from the queue
   #process it
   import time
   time.sleep(10)
   return True

I have created a Pool that creates 6 processes and executes the process_msg() function above.我创建了一个创建 6 个进程并执行上面的 process_msg() function 的池。 When the function times out, I want the Pool to call the function again and wait for new messages instead of exiting:当 function 超时时,我希望池再次调用 function 并等待新消息而不是退出:


if __name__ == "main":

 import multiprocessing
 from multiprocessing import Pool

 pool = Pool(processes=6)
 collection = range(6)
 try:
  val = pool.map_async(process_msg, collection)
  try:
       res = val.get(5)
  except TimeoutError:
       print('timeout here')
 pool.close()
 pool.terminate()
 pool.join()

The code runs and when I get a timeout, the application terminates itself.代码运行,当我超时时,应用程序自行终止。

What I want it to do is to print that the timeout as occurred and call the same function again.我想要它做的是打印发生的超时并再次调用相同的 function 。

What's the right approach?什么是正确的方法?

Here's a skeleton for a program that works.这是一个有效程序的框架。 The main issue you had is the use of pool.terminate , which "Stops the worker processes immediately without completing outstanding work" (see the documentation ).您遇到的主要问题是使用pool.terminate ,它“立即停止工作进程而不完成未完成的工作”(请参阅文档)。

from multiprocessing import Pool, TimeoutError
def process_msg(i):
   #get new message from the queue
   #process it
   import time
   print(f"Starting to sleep, proxess # {i}")
   time.sleep(10)
   return True

def main():
    print("in main")
    pool = Pool(processes=6)
    collection = range(6)
    print("About to spawn sub processes")
    val = pool.map_async(process_msg, collection)
    while True: 
        try:
            print("Waiting for results")
            res = val.get(3)
            print(f"Res is {res}")
            break
        except TimeoutError:
            print("Timeout here")

    print("Closing pool")
    pool.close()
    # pool.terminate() # do not terminate - it kill the child processes 
    print ("Joining pool")
    pool.join()
    print("exiting main")

if __name__ == "__main__":
    main()
    

The output of this code is:该代码的output为:

in main
About to spawn sub processes
Waiting for results
Starting to sleep, proxess # 0
Starting to sleep, proxess # 1
Starting to sleep, proxess # 2
Starting to sleep, proxess # 3
Starting to sleep, proxess # 4
Starting to sleep, proxess # 5
Timeout here
Waiting for results
Timeout here
Waiting for results
Timeout here
Waiting for results
Res is [True, True, True, True, True, True]
Closing pool
Joining pool
exiting main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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