Skip to main content

awaiter

codecov Quality Gate Status

Makes your blocking functions awaitable.

awaiter.ThreadExecutor() represents a single thread that you can use to execute blocking functions in a FIFO manner. It does not use a thread pool like asyncio.to_thread() or loop.run_in_executor(), to keep it minimal and predictable.

Usage

import asyncio
import time

from awaiter import ThreadExecutor


def blocking_function(name):
    time.sleep(1)
    return f'Hello, {name}!'

async def main():
    async with ThreadExecutor() as executor:
        result = await executor(blocking_function)('World')
        print(result)

    # out of context, the thread is closed here

if __name__ == '__main__':
    asyncio.run(main())

Or use the decorator style:

import asyncio
import time

from awaiter import ThreadExecutor

executor = ThreadExecutor()


@executor
def blocking_function(name):
    time.sleep(1)
    return f'Hello, {name}!'

async def main():
    executor.start()

    result = await blocking_function('World')
    print(result)

    executor.shutdown()
    # the thread will be closed.
    # if you want to wait until all queued tasks are completed:
    # await executor.shutdown()

if __name__ == '__main__':
    asyncio.run(main())

If you want to execute multiple tasks at once without waiting in the main thread, use executor.submit():

# ...

    fut1 = executor.submit(blocking_function, 'World')
    fut2 = executor.submit(blocking_function, 'Foo')
    fut3 = executor.submit(blocking_function, 'Bar')

# ...

Last but not least, it also supports generator functions:

# ...

@executor
def generator_function(name):
    yield 'Hello, '
    time.sleep(1)
    yield name
    yield '!'

# ...

    async for data in generator_function('World'):
        print(data)

# ...

But I want a thread pool?

We provide the awaiter.MultiThreadExecutor helper.

It has a thread pool-like approach and is more suitable for use as a single, persistent object:

executor = MultiThreadExecutor(size=10)

How to use is the same, as the interface is identical to awaiter.ThreadExecutor.

Install

python3 -m pip install --upgrade awaiter

License

MIT

Release files for awaiter 0.1.2

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

Source distribution (sdist)

Source distribution for awaiter 0.1.2
File Size Uploaded
awaiter-0.1.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for awaiter 0.1.2
File Interpreter ABI Platform
awaiter-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / awaiter-0.1.2.tar.gz

Download URL awaiter-0.1.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ebace0d7841be162f2735af42d95e113c7f05002c9d54ea99da21cd8619286c6
BLAKE2b-256 checksum
How to use checksums
5822e82b1298cc7261899710478bc4d2b6afc25a1074a535ae55763ba4fd1dbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.9

Release files / awaiter-0.1.2-py3-none-any.whl

Download URL awaiter-0.1.2-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b2a546732d92008b1afabda49210d7d8c49ea87d06489712e19065707f65255
BLAKE2b-256 checksum
How to use checksums
5e4d6cd23030488c2867f2defe78661d97e88e8a5c7a3ed28e5313cbd04a86bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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