Async library for Kivy
Project description
AsyncKivy
asynckivy is an async library that saves you from ugly callback-style code,
like most of async libraries do.
Let's say you want to do:
print('A')- wait for 1sec
print('B')- wait for a button to be pressed
print('C')
in that order. Your code would look like this:
from kivy.clock import Clock
def what_you_want_to_do(button):
print('A')
def one_sec_later(__):
print('B')
button.bind(on_press=on_button_press)
Clock.schedule_once(one_sec_later, 1)
def on_button_press(button):
button.unbind(on_press=on_button_press)
print('C')
what_you_want_to_do(...)
It's not easy to understand.
If you use asynckivy, the code above will become:
import asynckivy as ak
async def what_you_want_to_do(button):
print('A')
await ak.sleep(1)
print('B')
await ak.event(button, 'on_press')
print('C')
ak.managed_start(what_you_want_to_do(...))
Installation
Pin the minor version.
poetry add asynckivy@~0.9
pip install "asynckivy>=0.9,<0.10"
Usage
import asynckivy as ak
async def some_task(button):
# waits for 2 seconds to elapse
dt = await ak.sleep(2)
print(f'{dt} seconds have elapsed')
# waits for a button to be pressed
await ak.event(button, 'on_press')
# waits for the value of 'button.x' to change
__, x = await ak.event(button, 'x')
print(f'button.x is now {x}')
# waits for the value of 'button.x' to become greater than 100
if button.x <= 100:
__, x = await ak.event(button, 'x', filter=lambda __, x: x>100)
print(f'button.x is now {x}')
# waits for either 5 seconds to elapse or a button to be pressed.
# i.e. waits at most 5 seconds for a button to be pressed
tasks = await ak.wait_any(
ak.sleep(5),
ak.event(button, 'on_press'),
)
print("Timeout" if tasks[0].finished else "The button was pressed")
# same as the above
async with ak.move_on_after(5) as bg_task:
await ak.event(button, 'on_press')
print("Timeout" if bg_task.finished else "The button was pressed")
# waits for both 5 seconds to elapse and a button to be pressed.
tasks = await ak.wait_all(
ak.sleep(5),
ak.event(button, 'on_press'),
)
# nest as you want.
# waits for a button to be pressed, and either 5 seconds to elapse or 'other_async_func' to complete.
tasks = await ak.wait_all(
ak.event(button, 'on_press'),
ak.wait_any(
ak.sleep(5),
other_async_func(),
),
)
child_tasks = tasks[1].result
print("5 seconds elapsed" if child_tasks[0].finished else "other_async_func has completed")
ak.managed_start(some_task(some_button))
For more details, read the documentation.
Tested on
- CPython 3.10 + Kivy 2.3
- CPython 3.11 + Kivy 2.3
- CPython 3.12 + Kivy 2.3
- CPython 3.13 + Kivy 2.3
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 asynckivy-0.9.3.tar.gz.
File metadata
- Download URL: asynckivy-0.9.3.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ac45e310a398c588fab54e544082beda454d225f83cb372aa52650c2b5a2fd8
|
|
| MD5 |
63f41e292fb819a4733bc16a9251e31d
|
|
| BLAKE2b-256 |
c74eb8d60994d4e2dc4a76ab4c610ec1b13addfe9ad23c97040866bd262ced51
|
Provenance
The following attestation bundles were made for asynckivy-0.9.3.tar.gz:
Publisher:
release.yml on asyncgui/asynckivy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asynckivy-0.9.3.tar.gz -
Subject digest:
4ac45e310a398c588fab54e544082beda454d225f83cb372aa52650c2b5a2fd8 - Sigstore transparency entry: 763654721
- Sigstore integration time:
-
Permalink:
asyncgui/asynckivy@2a6497af88e658bfed073f47ad898021d27eff77 -
Branch / Tag:
refs/tags/v0.9.3 - Owner: https://github.com/asyncgui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2a6497af88e658bfed073f47ad898021d27eff77 -
Trigger Event:
push
-
Statement type:
File details
Details for the file asynckivy-0.9.3-py3-none-any.whl.
File metadata
- Download URL: asynckivy-0.9.3-py3-none-any.whl
- Upload date:
- Size: 23.0 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 |
db4eb69cba47af9e95842877fda5420ff08b7bc6498de1a30f0d198c48dca8c4
|
|
| MD5 |
1414d7a1e3d88fa0c6a01d424d90f9cf
|
|
| BLAKE2b-256 |
0eb3c965aad125df179e2b1010e33d6cc78c7514b485c41eefadcd962a159a35
|
Provenance
The following attestation bundles were made for asynckivy-0.9.3-py3-none-any.whl:
Publisher:
release.yml on asyncgui/asynckivy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asynckivy-0.9.3-py3-none-any.whl -
Subject digest:
db4eb69cba47af9e95842877fda5420ff08b7bc6498de1a30f0d198c48dca8c4 - Sigstore transparency entry: 763654725
- Sigstore integration time:
-
Permalink:
asyncgui/asynckivy@2a6497af88e658bfed073f47ad898021d27eff77 -
Branch / Tag:
refs/tags/v0.9.3 - Owner: https://github.com/asyncgui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2a6497af88e658bfed073f47ad898021d27eff77 -
Trigger Event:
push
-
Statement type: