2017-09-09 13 views
1

まず実行していない:私は限り近い私のコードを適応しようとした https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.htmlAiohttpは、すべての要求をHERESコードのすべての

:私はこのブログの記事、次のよ私の最後の質問を1として

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に印刷していません。

誰かがこのコードで何が間違っているのを発見できますか?

答えて

2

response.headersが定期的財産である一方、コール

asyncio.wait前のawaitを置く必要は先物のリストを受け入れないと(done, pending)ペアを返します。 await wait()await asyncio.gather(*tasks)gather doc

+0

と置き換えるべきであると思われる場合、どのようなシナリオで私は 'gather()'の代わりに 'wait()'を使うべきか説明できますか? – zython

+1

相対的に小さなタイムアウトで結果を収集する必要がある場合は、応答を処理し、未処理の結果を再度待機します。 –

関連する問題