futures.wait не прерывается в concurrent.futures в python3 .7 - PullRequest
0 голосов
/ 24 апреля 2020

У меня есть следующий код

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

Проблема в том, что тайм-аут никогда не возникает. Возвращается только когда все завершено. Что мне делать?

1 Ответ

0 голосов
/ 24 апреля 2020

Я изменил код, и теперь он, кажется, делает то, что я хочу. Теперь я понимаю, что это не выходит из контекста, пока все не закончится

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...