简体   繁体   English

Python多处理池超时

[英]python multiprocessing pool timeout

I want to use multiprocessing.Pool , but multiprocessing.Pool can't abort a task after a timeout. 我想使用multiprocessing.Pool ,但是multiprocessing.Pool不能在超时后中止任务。 I found solution and some modify it. 我找到了解决方案,并对其进行了一些修改。

from multiprocessing import util, Pool, TimeoutError
from multiprocessing.dummy import Pool as ThreadPool
import threading
import sys
from functools import partial
import time


def worker(y):
    print("worker sleep {} sec, thread: {}".format(y, threading.current_thread()))
    start = time.time()
    while True:
       if time.time() - start >= y:
           break
       time.sleep(0.5)
       # show work progress
       print(y)
    return y


def collect_my_result(result):
    print("Got result {}".format(result))


def abortable_worker(func, *args, **kwargs):
    timeout = kwargs.get('timeout', None)
    p = ThreadPool(1)
    res = p.apply_async(func, args=args)
    try:
        # Wait timeout seconds for func to complete.
        out = res.get(timeout)
    except TimeoutError:
        print("Aborting due to timeout {}".format(args[1]))
        # kill worker itself when get TimeoutError
        sys.exit(1)
    else:
        return out


def empty_func():
    pass


if __name__ == "__main__":
    TIMEOUT = 4
    util.log_to_stderr(util.DEBUG)
    pool = Pool(processes=4)

    # k - time to job sleep
    featureClass = [(k,) for k in range(20, 0, -1)]  # list of arguments
    for f in featureClass:
        # check available worker
        pool.apply(empty_func)

        # run job with timeout
        abortable_func = partial(abortable_worker, worker, timeout=TIMEOUT)
        pool.apply_async(abortable_func, args=f, callback=collect_my_result)

    time.sleep(TIMEOUT)
    pool.terminate()
    print("exit")

main modification - worker process exit with sys.exit(1) . 主要修改-使用sys.exit(1)退出工作进程。 It's kill worker process and kill job thread, but i'm not sure that this solution is good. 它杀死了工作进程并杀死了工作线程,但是我不确定这个解决方案是否很好。 What potential problems can i get, when process terminate itself with running job? 当进程因正在运行的作业而终止时,我会遇到哪些潜在的问题?

There is no implicit risk in stopping a running job, the OS will take care of correctly terminating the process. 停止正在运行的作业没有隐含的风险,操作系统将负责正确终止进程。

If your job is writing on files, you might end up with lots of truncated files on your disk. 如果您的工作是写文件,则磁盘上可能会有很多被截断的文件。

Some small issue might also occur if you write on DBs or if you are connected with some remote process. 如果您在数据库上编写或与某个远程进程连接,则也可能会出现一些小问题。

Nevertheless, Python standard Pool does not support timeouts and terminating processes abruptly might lead to weird behaviour within your applications. 不过,Python标准池不支持超时,并且突然终止进程可能会导致应用程序内部出现异常行为。

Pebble processing Pool does support timing-out tasks. Pebble处理池确实支持超时任务。

from pebble import process, TimeoutError

with process.Pool() as pool:
    task = pool.schedule(function, args=[1,2], timeout=5)

    try:
        result = task.get()
    except TimeoutError:
        print "Task: %s took more than 5 seconds to complete" % task

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

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