question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to make it work with asyncio?

See original GitHub issue

I have a CLI tool fetching an unknown number of items over an unknown number of pages from an API. The tools uses asyncio (python 3.7) to fetch the data and process it.

I would like to use this progress bar, but when I do, I am facing 2 issues:

  1. I have to use sort of a workaround to count the number of items processed asynchronously (see example below).
  2. The bar looks super sluggish which does not provide a great user experience.

Here is a piece of code to simulate my problem with the same symptoms:

import asyncio
import random

from alive_progress import alive_bar


async def fetch_and_process_page(page_number):
    await asyncio.sleep(random.randrange(3))
    return 1


async def main():
    page = 1
    res = []
    with alive_bar(0, spinner="waves") as bar:
        while True:
            tasks = [
                fetch_and_process_page(page)
                for _ in range(random.randrange(5))
            ]
            res += await asyncio.gather(*tasks)

            # Workaround to get the progress bar working.
            for _ in range(len(tasks)):
                bar()

            if random.randrange(12) > 10:
                break
            page += 1
    print(f"{len(res)} items collected.")


asyncio.run(main())

Am I using the progress bar correctly? I could not find information regarding its usage with asyncio.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rsalmeicommented, Sep 17, 2019

It’s alive @rgreinho ! Please install the latest version and let me know! 😅

1reaction
rsalmeicommented, Sep 17, 2019

Ah, also read the new corresponding readme section: https://github.com/rsalmei/alive-progress#calibration--new

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async IO in Python: A Complete Walkthrough
This tutorial will give you a firm grasp of Python's approach to async IO, which is a concurrent programming design that has received...
Read more >
How does asyncio actually work? - Stack Overflow
The event loop's job is to call tasks every time they are ready and coordinate all that effort into one single working machine....
Read more >
Python Asyncio: The Complete Guide
This guide provides a detailed and comprehensive review of asyncio in Python, including how to define, create and run coroutines, what is ...
Read more >
Python Asyncio Part 2 – Awaitables, Tasks, and Futures
The method create_task takes a coroutine object as a parameter and returns a Task object, which inherits from asyncio. Future . The call...
Read more >
Python Concurrency: Making sense of asyncio - Educative.io
Note: If you are working with Python 3.5, then the asyncio.run() API isn't available. In that case, you explicitly retrieve the event loop...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found