我想非常快速地连接到很多不同站点的列表。我使用 asyncio 以异步方式执行此操作,现在想要添加超时,用于在连接响应时间过长时应忽略连接。
我该如何实现?
import ssl
import asyncio
from contextlib import suppress
from concurrent.futures import ThreadPoolExecutor
import time
@asyncio.coroutine
def run():
while True:
host = yield from q.get()
if not host:
break
with suppress(ssl.CertificateError):
reader, writer = yield from asyncio.open_connection(host[1], 443, ssl=True) #timout option?
reader.close()
writer.close()
@asyncio.coroutine
def load_q():
# only 3 entries for debugging reasons
for host in [[1, 'python.org'], [2, 'qq.com'], [3, 'google.com']]:
yield from q.put(host)
for _ in range(NUM):
q.put(None)
if __name__ == "__main__":
NUM = 1000
q = asyncio.Queue()
loop = asyncio.get_event_loop()
loop.set_default_executor(ThreadPoolExecutor(NUM))
start = time.time()
coros = [asyncio.async(run()) for i in range(NUM)]
loop.run_until_complete(load_q())
loop.run_until_complete(asyncio.wait(coros))
end = time.time()
print(end-start)
(附带说明:有人知道如何优化它吗?
您可以将对open_connection
的调用包装在 asyncio.wait_for
中,这允许您指定超时:
with suppress(ssl.CertificateError):
fut = asyncio.open_connection(host[1], 443, ssl=True)
try:
# Wait for 3 seconds, then raise TimeoutError
reader, writer = yield from asyncio.wait_for(fut, timeout=3)
except asyncio.TimeoutError:
print("Timeout, skipping {}".format(host[1]))
continue
请注意,引发TimeoutError
时,open_connection
协程也会被取消。如果您不希望它被取消(尽管我认为您确实希望在这种情况下取消它),您将呼叫包装在 asyncio.shield
中。