Skip to main content

aioawait

This package implements two primitives (await and spawn) on top of asyncio infrastructure of Python 3. This two functions allow us to call asynchronous functions from synchronous code.

Installation

pip3 install aioawait

Example

from asyncio.tasks import coroutine, sleep, async
from aioawait import await, spawn

@coroutine
def monitor(name, size, total):
    while True:
        print('\ttotal', name, total)
        yield from sleep(1)

@coroutine
def counter(name, size, total):
    """sums into total all numbers from 0 to size"""
    m = async(monitor(name, size, total))

    # monitor could be called using spawn. eg:
    # m = spawn(monitor(name, size, total))

    for n in range(size):
        total[0] += n
        if n % 5 == 0:
            print('sleeping', name, n)
            yield from sleep(2)
        else:
            print('counting', name, n)
            yield

    # stops monitor
    m.cancel()

    return name, 'done', n, total

class Counter:
    """note that this class has no asynchronous code"""

    def __init__(self):
        self.cb = spawn(counter('b', 40, [0]))

    @property
    def counter_a(self):
        return await(counter('a', 20, [0]))

    @property
    def counter_b(self):
        return await(self.cb)

if __name__ == '__main__':
    c = Counter()
    print(c.counter_a)
    print(c.counter_b)

Release files for aioawait 1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioawait 1
File Size Uploaded
aioawait-1.tar.gz 2.4 kB Details

Release files / aioawait-1.tar.gz

Download URL aioawait-1.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4345d17ecbd211b658ad492bec30d8f82c27edbe72dd040e390e0cae73085844
BLAKE2b-256 checksum
How to use checksums
6664fdedd760f8bf8b711e3f507fa4eb382aeea805c0dbb6a332d6a9b1ac0968
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

8

1 release file

7

1 release file

5

1 release file

3

1 release file

2

1 release file

This release

1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page