gpt4 book ai didi

python - Aiohttp 不执行任何请求

转载 作者:行者123 更新时间:2023-12-01 02:36:47 24 4
gpt4 key购买 nike

首先是代码:

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 中打印 header 。

有人能发现这段代码有什么问题吗?

最佳答案

response.headers是常规属性,无需在调用前放置await

asyncio.wait另一方面,接受 future 列表并返回 (done, pending) 对。看来您应该将 await wait() 调用替换为 await asyncio.gather(*tasks) ( gather doc )

关于python - Aiohttp 不执行任何请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46132688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com