在python3.7的concurrent.futures中,futures.wait没有超时。

我有以下代码

def multiprocess(dbs, tables, timeout=None):
    """
    Run all the processes for each table at the same time
    :param dbs: list of database objects
    :param tables: list: tables to work on
    :param timeout: timeout time
    :return: list of results
    """
    # Sort the source
    results = []
    with ThreadPoolExecutor(max_workers=30) as thread:
        jobs = {thread.submit(procedure, db, table) for db in dbs for table in tables}
        done, running = futures.wait(jobs, timeout=1, return_when=futures.ALL_COMPLETED)

    for job in jobs:
        if job.done():
            results.append(job.result())

    return results

问题是超时从未发生。只有在全部完成后才会返回。我应该怎么做?

解决方案:

我修改了代码,现在似乎可以做到我想要的。我现在明白了,它不会离开上下文,直到所有的事情都完成了。

def multiprocess(dbs, tables, timeout=0):
    """
    Run all the processes for each table at the same time
    :param dbs: list of database objects
    :param tables: list: tables to work on
    :param timeout: timeout time
    :return: list of results
    """
    with ThreadPoolExecutor(max_workers=20) as thread:
        jobs = {thread.submit(procedure, db, table) for db in dbs for table in tables}
        done, not_done = futures.wait(jobs, timeout=timeout)
        print(f"Done {done}")
        for job in done:
            yield job.result()

    print(f"Not Done {not_done}")
    for job in not_done:
        yield job.result()

给TA打赏
共{{data.count}}人
人已打赏
未分类

boost::bind()会创建许多参数的副本。

2022-9-9 5:08:17

未分类

安卓。在回收器视图中点击图片按钮时,从Textview中传递文本。

2022-9-9 5:08:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索