Skip to main content

A thin layer that helps to build async/await-based APIs using callback-based APIs

Project description

AsyncGui

A thin layer that helps to wrap a callback-style API in an async/await-style API.

How to use

Despite its name, the application of asyncgui is not limited to gui programs. You can wrap any kind of callback-based APIs in it. The simplest example of it would be sched, whose the whole feature is timer. All you need is just few lines of code:

import sched
import asyncgui

s = sched.scheduler()

# Wrapping 'scheduler.enter()'
async def sleep(duration, *, priority=10):
    sig = asyncgui.ISignal()
    event = s.enter(duration, priority, sig.set)
    try:
        await sig.wait()
    except asyncgui.Cancelled:
        s.cancel(event)
        raise


async def main():
    print('A')
    await sleep(1)  # Now you can sleep in an async-manner
    print('B')
    await sleep(1)
    print('C')


asyncgui.start(main())
s.run()

And you already have structured concurrency APIs as well:

async def print_numbers():
    for i in range(10):
        await sleep(.1)
        print(i)


async def print_letters():
    for c in "ABCDE":
        await sleep(.1)
        print(c)


async def main():
    # Let print_letters() and print_numbers() race.
    # As soon as any of them finishes, the other one gets cancelled.
    tasks = await asyncgui.wait_any(print_letters(), print_numbers())
    if tasks[0].finished:
        print("print_letters() won")
    else:
        print("print_numbers() won")
    print('main end')
A
0
B
1
C
2
D
3
E
print_letters() won
main end

Installation

It's recommended to pin the minor version, because if it changed, it means some important breaking changes occurred.

poetry add asyncgui@~0.6
pip install "asyncgui>=0.6,<0.7"

Tested on

  • CPython 3.8
  • CPython 3.9
  • CPython 3.10
  • CPython 3.11
  • CPython 3.12 (3.12.1 or later)

Async-libraries who relies on this

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

asyncgui-0.6.1.tar.gz (10.4 kB view hashes)

Uploaded Source

Built Distribution

asyncgui-0.6.1-py3-none-any.whl (9.9 kB view hashes)

Uploaded Python 3

Supported by

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