首页 > 解决方案 > Python重复了ThreadPoolExecutor的某些任务

问题描述

我正在尝试从 concurrent.futures 玩 ThreadPoolExecutor。

我的代码如下所示:

def f(x) 情况 1:返回 False 情况 2:返回 True

def runner(x):
    out = set()
    threads = []
    export = []

    while len(x)>0:
        if len(x)<21:
        workers = len(x)
    else:
        workers =20

    with ThreadPoolExecutor(max_workers=workers) as executor:
        for v in x:
            threads.append(executor.submit(my_job, v))
    
        for task in as_completed(threads):
            export_dict =task.result()
            if export_dict:
                export.append(export_dict)
            else:
                out.add(number)

        x = list(out)

    return export, out

while len(un_done)>0:
    export, un_done = runner(un_done)
    print(un_done)
    #write_mongo(db_name='CDR',coll_name='cdr',v=to_cdr)
    write_mongo(db_name='NumberHistory', coll_name='numberhistory', v=export)

我的意图是在一个新线程上重复所有失败的任务。所以我想重复 runner 函数,直到我从所有 f(x) 中得到 True。我最终得到了很多永远不会结束的重复性任务。

标签: python-3.xmultithreadingthreadpoolexecutor

解决方案


推荐阅读