Aiohttp 不执行任何请求

Aiohttp not performing any requests

首先是代码:

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()

根据我的最后一个问题,我正在关注此博客 post: https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html

我尝试使我的代码尽可能接近示例实现,但此代码仍未发出任何请求并按我的意愿在 bound_headers 中打印 headers。

有人能看出这段代码有什么问题吗?

response.headers是常规的属性,调用前不需要放await

另一方面,

asyncio.wait 接受期货列表和 returns (done, pending) 对。 看起来您应该将 await wait() 调用替换为 await asyncio.gather(*tasks) (gather doc)