Python 3.x 如果其中一个线程产生结果,我可以停止等待线程完成吗?

Python 3.x 如果其中一个线程产生结果,我可以停止等待线程完成吗?,python-3.x,multithreading,performance,asynchronous,concurrency,Python 3.x,Multithreading,Performance,Asynchronous,Concurrency,我向不同服务器上大约几百个不同的API端点发出了一系列GET请求。在其中一个端点中,有一些我想要获取并返回的信息 在这些请求中的任何一个向我返回某些内容之后,我想终止其他线程并退出。有些请求几乎是即时的,有些可能需要20秒才能完成 如果我碰巧在2秒钟内找到了信息,我不想在20秒钟后才能恢复工作 目前我正在做这样的事情: threads = list() for s in silos: #here i create all the requests t = Thread(target=pr

我向不同服务器上大约几百个不同的API端点发出了一系列GET请求。在其中一个端点中,有一些我想要获取并返回的信息

在这些请求中的任何一个向我返回某些内容之后,我想终止其他线程并退出。有些请求几乎是即时的,有些可能需要20秒才能完成

如果我碰巧在2秒钟内找到了信息,我不想在20秒钟后才能恢复工作

目前我正在做这样的事情:

threads = list()
for s in silos: #here i create all the requests
    t = Thread(target=process_request, args=(my, args, here))
    t.name = "{} - {}".format(some, name)
    threads.append(t)
那么我会:

print("Threads: {}".format(len(threads))) # 100 - 250 of them
    [ t.start() for t in threads ]
    [ t.join() for t in threads ]
process_request()只发出get请求,如果状态_code==200,则将结果存储在dict中。
我正在使用请求和线程模块。

如果使用多进程池,则可以在第一个响应到达时立即终止池:

将多处理导入为mp
导入时间
池=无
def make_get_请求(输入):
打印('使用输入发出get请求'+str(输入))
时间。睡眠(2)
返回“输入的虚拟响应”+str(输入)
def日志_响应(响应):
打印(“获得响应=“+响应”)
pool.terminate()
def main():
全球池
pool=mp.pool()
对于范围(10)内的i:
apply\u async(make\u get\u请求,args=(i,),callback=log\u响应)
pool.close()
pool.join()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()