Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rob-blackbourn/countdown-event
An asyncio event which blocks until the count reaches zero
https://github.com/rob-blackbourn/countdown-event
asyncio python
Last synced: about 1 month ago
JSON representation
An asyncio event which blocks until the count reaches zero
- Host: GitHub
- URL: https://github.com/rob-blackbourn/countdown-event
- Owner: rob-blackbourn
- License: apache-2.0
- Created: 2019-08-16T10:46:59.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-08-16T10:51:25.000Z (over 5 years ago)
- Last Synced: 2024-09-29T09:25:43.994Z (about 2 months ago)
- Topics: asyncio, python
- Language: Python
- Size: 8.79 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# countdown-event
A synchronization class which blocks when the count is not zero.
## Usage
Here's an example
```python
import asyncio
from countdown_event import CountdownEventasync def long_running_task(countdown_event,cancellation_event):
count = countdown_event.increment()
print(f'incremented count to {count}')
try:
print('Waiting for cancellation event')
await cancellation_event.wait()
finally:
count = countdown_event.decrement()
print(f'decremented count to {count}')async def stop_tasks(secs, countdown_event, cancellation_event):
print(f'waiting {secs} seconds before setting the cancellation event')
await asyncio.sleep(secs)
print('setting the cancellation event')
cancellation_event.set()
print('waiting for tasks to finish')
await countdown_event.wait()
print('countdown event cleared')async def main_async():
cancellation_event = asyncio.Event()
countdown_event = CountdownEvent()
tasks = [
long_running_task(countdown_event, cancellation_event),
long_running_task(countdown_event, cancellation_event),
long_running_task(countdown_event, cancellation_event),
stop_tasks(5, countdown_event, cancellation_event)
]
await asyncio.wait(tasks)
assert countdown_event.count == 0
print("done")if __name__ == "__main__":
asyncio.run(main_async())
```Here's the output.
```
incremented count to 1
Waiting for cancellation event
incremented count to 2
Waiting for cancellation event
waiting 5 seconds before setting the cancellation event
incremented count to 3
Waiting for cancellation event
setting the cancellation event
waiting for tasks to finish
decremented count to 2
decremented count to 1
decremented count to 0
countdown event cleared
done
```## Installation
Install from pypi.
```bash
$ pip install countdown-event
```