rich+asyncio #1401
-
What about using rich.live with asyncio? This code miserably fails, because it instances Live() more than once. -> https://bpa.st/2FZA My aim here, is a very simple thing, that a library like rich should easily let to do:
This is like having N progress bars that are printed in the console, while also other text is printed in the console, and those progress bars still continue their job. Is this possible with current rich capabilities? It would be very useful to support such as behaviors within asyncio! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You can use Live with asyncio, but you would structure it differently. Have a single instance of Live running in a thread. Your tasks can run in another thread and call You could also use a Progress class which has support for multiple jobs (you could replace the bars with a spinner). |
Beta Was this translation helpful? Give feedback.
-
something like this? import sys
import asyncio
from rich.live import Live
from rich.table import Table
async def do_task(name: str, live: Live, table: Table) -> None:
for row in range(12):
live.console.print(f"Working on row #{row} {name}")
await asyncio.sleep(2.0)
table.add_row(f"{row}", f"description {row} {name}", "[red]ERROR")
async def amain() -> int:
table = Table()
table.add_column("Row ID")
table.add_column("Description")
table.add_column("Level")
with Live(
table, refresh_per_second=4
) as live: # update 4 times a second to feel fluid
await asyncio.gather(do_task("BAU", live, table), do_task("MIAO", live, table))
return 0
def main() -> int:
return asyncio.run(amain())
if __name__ == "__main__":
sys.exit(main()) |
Beta Was this translation helpful? Give feedback.
You can use Live with asyncio, but you would structure it differently. Have a single instance of Live running in a thread. Your tasks can run in another thread and call
live.update
when there is a new table.You could also use a Progress class which has support for multiple jobs (you could replace the bars with a spinner).