Python Aiohttp不执行任何请求
首先,代码如下:Python Aiohttp不执行任何请求,python,asynchronous,aiohttp,Python,Asynchronous,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
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()
根据我的最后一个问题,我将关注这篇博文:
我试图使我的代码尽可能接近示例实现,但这段代码仍然没有发出任何请求,也没有按照我的意愿在bound_headers
中打印头
有人能发现这个代码有什么问题吗?是一个常规属性,不需要在调用前放置wait
另一方面,接受期货和收益的列表(完成,待定)
对。
看起来您应该用
await asyncio.gather(*tasks)
()替换await wait()
调用。有道理,您能解释一下在什么情况下我应该使用wait(),正在处理准备好的答案,并再次等待尚未处理的结果。