An architecture to easily run asyncio tasks from Click
Project description
click-async-plugins
This is a proof-of-concept of a Python asyncio plugin architecture based on click.
Instead of writing functions that make up sub-commands, you write asynchronous "lifespan functions" (AsyncGenerator), like this one:
@cli_core.plugin_command
@click.option("--sleep", type=click.FloatRange(min=0.01), default=1)
async def myplugin(sleep: float) -> PluginLifespan:
# code to set things up goes here
async def long_running_task(*, sleep: float):
# task initialisation can happen here
try:
while True:
await asyncio.sleep(sleep)
finally:
# code to clean up the task goes here
pass
yield long_running_task(sleep=sleep)
# code to tear things down goes here
Multiple such plugins can be defined/added to core (see the demo code). These plugins will all have their setup code called in turn. If, after setup, a plugin yields a coroutine (e.g. a long-running task), this task is scheduled with the main event loop, but this is optional, and tasks that yield nothing (None) will just sleep until program termination. Upon termination, the plugins' teardown code is invoked (in reverse order).
Here's what the demo code logs to the console. Two plugins are invoked. The first counts down from 3 each second, and notifies subscribers of each number. The second plugin — "echo" — just listens for updates from the "countdown" task and echoes them.
$ python demo.py countdown --from 3 echo --immediately
DEBUG:root:Setting up task for 'echo'
DEBUG:root:Setting up task for 'countdown'
DEBUG:root:Scheduling task for 'echo'
DEBUG:root:Waiting for update to 'countdown'…
DEBUG:root:Scheduling task for 'countdown'
INFO:root:Counting down… 3
DEBUG:root:Notifying subscribers of update to 'countdown'…
INFO:root:Countdown currently at 3
DEBUG:root:Waiting for update to 'countdown'…
INFO:root:Counting down… 2
DEBUG:root:Notifying subscribers of update to 'countdown'…
INFO:root:Countdown currently at 2
DEBUG:root:Waiting for update to 'countdown'…
INFO:root:Counting down… 1
DEBUG:root:Notifying subscribers of update to 'countdown'…
INFO:root:Countdown currently at 1
DEBUG:root:Waiting for update to 'countdown'…
INFO:root:Finished counting down
^C
DEBUG:root:Task for 'echo' cancelled
DEBUG:root:Terminating…
DEBUG:root:Lifespan over for countdown
DEBUG:root:Lifespan over for echo
DEBUG:root:Finished.
I hope you get the idea. If you need more input, take a look at look at tptools' tpsrv CLI, which I was developing when I factored out this code.
There's also a "debug" plugin included, which allows interaction with the CLI as
its running via key presses. Hit ? to get an overview of commands available.
Looking forward to your feedback.
Oh, and if someone wanted to turn this into a proper package with tests and everything, I think it could be published to pip/pypy. I need to stop shaving this yak now, though.
© 2025 martin f. krafft <click-async-plugins@pobox.madduck.net>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file click_async_plugins-0.7.3.tar.gz.
File metadata
- Download URL: click_async_plugins-0.7.3.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ce428974ec893572ee047ed848d414ff08e4965fe4dd331231ce82b935a2e21
|
|
| MD5 |
84b12f061d6475f7ddf42ecead5f12ee
|
|
| BLAKE2b-256 |
2ae1d7dc78040cfe925456d6c04d59c80d855e7a6be793205fb756f91185fe33
|
Provenance
The following attestation bundles were made for click_async_plugins-0.7.3.tar.gz:
Publisher:
python-package.yml on madduck/click-async-plugins
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click_async_plugins-0.7.3.tar.gz -
Subject digest:
1ce428974ec893572ee047ed848d414ff08e4965fe4dd331231ce82b935a2e21 - Sigstore transparency entry: 1107686330
- Sigstore integration time:
-
Permalink:
madduck/click-async-plugins@c4eb2e22b235bada60fbd8417a36e40210285fda -
Branch / Tag:
refs/heads/main - Owner: https://github.com/madduck
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@c4eb2e22b235bada60fbd8417a36e40210285fda -
Trigger Event:
push
-
Statement type:
File details
Details for the file click_async_plugins-0.7.3-py3-none-any.whl.
File metadata
- Download URL: click_async_plugins-0.7.3-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e241c385c765771daf0422bdd72e1a42b69a28afa0d757b237e1dffe9711875
|
|
| MD5 |
a2d5d2318e97ea7b6a38eb7f2b7c1996
|
|
| BLAKE2b-256 |
08a580635d6e1589d6d4cbc85fcc1fad4523d21001caada5e5c3a19f11d9533a
|
Provenance
The following attestation bundles were made for click_async_plugins-0.7.3-py3-none-any.whl:
Publisher:
python-package.yml on madduck/click-async-plugins
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click_async_plugins-0.7.3-py3-none-any.whl -
Subject digest:
0e241c385c765771daf0422bdd72e1a42b69a28afa0d757b237e1dffe9711875 - Sigstore transparency entry: 1107686331
- Sigstore integration time:
-
Permalink:
madduck/click-async-plugins@c4eb2e22b235bada60fbd8417a36e40210285fda -
Branch / Tag:
refs/heads/main - Owner: https://github.com/madduck
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@c4eb2e22b235bada60fbd8417a36e40210285fda -
Trigger Event:
push
-
Statement type: