在使用current.futures.ThreadPoolExecutor发出Web请求时,所有线程似乎都在等待一个响应块时等待。
据我了解,Python线程不会像C / C ++中的线程那样分配给单独的内核,但是我很好奇这种情况是否适用于它们。
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=5)
for item in items:
executor.submit(make_request,item)
executor.shutdown(wait=True)
return
def make_request(item):
#make request using item info
if worked:
print("success")
else:
print("failed")
return
例如,如果我使用5个工作线程,则所有5个请求都会接连发出请求(似乎比使用1个工作线程时要快),但是当一个请求花费很长时间或超时时,其他请求似乎会停止并等待。我希望,如果一个人等待5秒钟进行响应,而其他人已经完成,其余的将继续发出新请求并继续前进。
我误解了Python线程如何工作?也许我使用的ThreadPoolExecutor不正确。