Skip to main content

Makes your blocking functions awaitable.

Project description

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

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

awaiter-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

awaiter-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file awaiter-0.1.0.tar.gz.

File metadata

  • Download URL: awaiter-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for awaiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c82f24d5c9da2301e6f950c7fcf391e0ff67c47718071160b2d0411fcc9d5fec
MD5 acd97a5c759cce1528c55d83a791e6cb
BLAKE2b-256 d76f5d3cc65d7aec6e231219f4a6a9f85b1608d9dd2d8a56431d22414e160716

See more details on using hashes here.

File details

Details for the file awaiter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: awaiter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for awaiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 842436ed0d6f19e62e2d11a2a02470a9e1017f01d6d19dcea660c38dd0780cbd
MD5 0f4623240fd54e87a719094c93745226
BLAKE2b-256 bc51a0a9c753c35961578246d0a3c4dbcc45904587768890ee0001dba33f20ac

See more details on using hashes here.

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