Skip to main content

Async library for Kivy

Project description

AsyncKivy

Youtube
日本語doc

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:

  1. print('A')
  2. wait for 1sec
  3. print('B')
  4. wait for a button to be pressed
  5. 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.

pip install "asynckivy>=0.10,<0.11"

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asynckivy-0.10.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asynckivy-0.10.0-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file asynckivy-0.10.0.tar.gz.

File metadata

  • Download URL: asynckivy-0.10.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asynckivy-0.10.0.tar.gz
Algorithm Hash digest
SHA256 3897759cd3a9aae3c2e1ce3418cbf32f5b3dc152a68d97bbaf7a0e2d01aa1ccf
MD5 85538faf40b6fd93006cbe63db09527b
BLAKE2b-256 27b55202cb5a9a09973dcbc7807f875e5d4e8e95b3b2d65843a25c7e6de6c9df

See more details on using hashes here.

Provenance

The following attestation bundles were made for asynckivy-0.10.0.tar.gz:

Publisher: release.yml on asyncgui/asynckivy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file asynckivy-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: asynckivy-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asynckivy-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7d51152b4ccd00bba7525be56b7e7e65dca072860a8b231c91bb6ea2672845b
MD5 4fc8f6c2006a917021a85a149679057d
BLAKE2b-256 f25467c449471d86e2ff247960d4c43908f8e18999f6fb4aef1f2f94d1a5e8cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for asynckivy-0.10.0-py3-none-any.whl:

Publisher: release.yml on asyncgui/asynckivy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page