1
首先繼承人的代碼:Aiohttp不執行任何請求
import random
import asyncio
from aiohttp import ClientSession
import csv
headers =[]
def extractsites(file):
sites = []
readfile = open(file, "r")
reader = csv.reader(readfile, delimiter=",")
raw = list(reader)
for a in raw:
sites.append((a[1]))
return sites
async def bound_fetch(sem, url):
async with sem:
print("doing request for "+ url)
async with ClientSession() as session:
async with session.get(url) as response:
responseheader = await response.headers
print(headers)
async def run():
urls = extractsites("cisco-umbrella.csv")
tasks = []
sem = asyncio.Semaphore(100)
for i in urls:
task = asyncio.ensure_future(bound_fetch(sem, "http://"+i))
tasks.append(task)
headers = await asyncio.wait(*tasks)
print(headers)
def main():
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run())
loop.run_until_complete(future)
if __name__ == '__main__':
main()
按我的最後一個問題,我下面的這篇博客文章: https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html
我試着去適應我的代碼儘可能接近可能的示例實現,但這個代碼仍然沒有發出任何請求,並按照我的意願在bound_headers
中打印標題。
有人可以發現這個代碼有什麼問題嗎?
是有道理的,你可以在我應該使用什麼樣的情況說明'等待()',而不是'聚集()'? – zython
當您需要收集具有相對較小超時的結果時,處理就緒答案並再次等待未處理的結果。 –