【问题标题】:How to start a pool of threads and stop when one is finished in python?如何在python中启动一个线程池并在一个线程完成时停止?
【发布时间】:2022-01-19 21:06:28
【问题描述】:

我正在尝试编写一个同时发送多个请求并返回第一个响应的函数。我目前正在使用concurrent.futures.ThreadPoolExecutor 对象。我知道在中间停止请求很复杂,所以我认为我可以将其他线程保留在后台并尽早返回一个值。但是,该函数似乎在返回之前等待其他线程完成。我怎样才能防止这种情况?我的代码如下所示:

def req(urls):
    with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = []
        for url in urls:
            futures.append(executor.submit(get_request, url))
        for future in concurrent.futures.as_completed(futures):
            if future.result():
                return future.result()  # Other threads should stop now
    return False  # No valid response was sent

【问题讨论】:

  • 由于GIL,I/O 操作的多线程不是 Python 中的方式。通过asyncio 查看异步 I/O。见here

标签: python multithreading concurrent.futures


【解决方案1】:

尝试关机https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.shutdown

def req(urls):
    with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = []
        for url in urls:
            futures.append(executor.submit(get_request, url))
        for future in concurrent.futures.as_completed(futures):
            if future.result():
                executor.shutdown(wait=False, cancel_futures=True)
                return future.result()  # Other threads should stop now
    return False  # No valid response was sent

【讨论】:

  • 不幸的是,我的 python 似乎已经过时了,我无法使用cancel_futures 选项。
  • 你也可以遍历所有的期货并在每个期货上调用.cancel()
  • cancel 返回 False,因为线程仍处于挂起状态。
猜你喜欢
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
相关资源
最近更新 更多