Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed Feb 12, 2024
1 parent 286d3e7 commit f31702c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pip install tqdm_publisher
## Getting Started

### Basic Usage
To convert an existing TQDM progress bar into a `TQDMPublisher`, simply swap the `tqdm` and `TQDMPublisher` constructors. Then, before iterating, subscribe to updates from the `TQDMPublisher` instance using the `subscribe` method.
To convert an existing TQDM progress bar into a `TQDMPublisher`, simply swap the `tqdm` and `TQDMPublisher` constructors.

You'll need to declare a callback function to handle progress updates, which you can declare wherever you like.

Then, before iterating, subscribe your callback to the `TQDMPublisher` updates using the `subscribe` method.

#### Original Code
```python
Expand Down Expand Up @@ -59,7 +63,7 @@ from tqdm_publisher import TQDMPublisher
async def sleep_func(sleep_duration = 1):
await asyncio.sleep(delay=sleep_duration)

async def run_multiple_sleeps(sleep_durations):
async def run_multiple_sleeps(sleep_durations, on_update):

tasks = []

Expand All @@ -71,7 +75,7 @@ async def run_multiple_sleeps(sleep_durations):
progress_bar = TQDMPublisher(asyncio.as_completed(tasks), total=len(tasks))

# Subscribe to updates from the progress bar
callback_id = progress_bar.subscribe(lambda info: print('Progress Update', info))
callback_id = progress_bar.subscribe(on_update)

# Start the tasks
for f in progress_bar:
Expand All @@ -80,9 +84,14 @@ async def run_multiple_sleeps(sleep_durations):
# Unsubscribe from updates (optional)
progress_bar.unsubscribe(callback_id)

# Define a callback function to handle progress updates
main_callback = lambda info: print('Progress Update', info)

number_of_tasks = 10**5
sleep_durations = [random.uniform(0, 5.0) for _ in range(number_of_tasks)]
asyncio.run(run_multiple_sleeps(sleep_durations=sleep_durations))

# Pass your callback function to the coroutine
asyncio.run(run_multiple_sleeps(sleep_durations=sleep_durations, on_update=main_callback))
```

## Demo
Expand Down

0 comments on commit f31702c

Please sign in to comment.