asyncio aiohttp progress bar with tqdm
<p>I'm attempting to integrate a <code>tqdm</code> progress bar to monitor POST requests generated with <code>aiohttp</code> in Python 3.5. I have a working progress bar but can't seem to gather results using <code>as_completed()</code>. Pointers gratefully received.</p>

<p>Examples I've found suggest using the following pattern, which is incompatible with Python 3.5 <code>async def</code> definitions:</p>

<pre><code>for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(coros)):
    yield from f
</code></pre>

<p>Working (albeit redacted) async code without the progress bar:</p>

<pre><code>def async_classify(records):

    async def fetch(session, name, sequence):
        url = 'https://app.example.com/api/v0/search'
        payload = {'sequence': str(sequence)}
        async with session.post(url, data=payload) as response:
            return name, await response.json()

    async def loop():
        auth = aiohttp.BasicAuth(api_key)
        conn = aiohttp.TCPConnector(limit=100)
        with aiohttp.ClientSession(auth=auth, connector=conn) as session:
            tasks = [fetch(session, record.id, record.seq) for record in records]
            responses = await asyncio.gather(*tasks)    
        return OrderedDict(responses)
</code></pre>

<p>This is my unsuccessful attempt at modifying <code>loop()</code>:</p>

<pre><code>async def loop():
    auth = aiohttp.BasicAuth(api_key)
    conn = aiohttp.TCPConnector(limit=100)
    with aiohttp.ClientSession(auth=auth, connector=conn) as session:
        tasks = [fetch(session, record.id, record.seq) for record in records]
        for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)):
            await f
        responses = await asyncio.gather(f)
        print(responses)
</code></pre>