Question: How to make it work with asyncio?
See original GitHub issueI 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:
- I have to use sort of a workaround to count the number of items processed asynchronously (see example below).
- 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:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s alive @rgreinho ! Please install the latest version and let me know! 😅
Ah, also read the new corresponding readme section: https://github.com/rsalmei/alive-progress#calibration--new