Skip to main content

you can have a little async (as a treat)

build status PyPI status

asyncio-thread-runner allows you to run async code from sync code.

This is useful when you're doing some sync stuff, but:

  • you also need to do some async stuff, without making everything async
  • maybe the sync stuff is an existing application
  • maybe you still want to use your favorite sync library
  • or maybe you need just a little async, without having to pay the full price

Features:

  • unlike asyncio.run(), it provides a long-lived event loop
  • unlike asyncio.Runner, it runs in a dedicated thread, and you can use it from multiple threads
  • it allows you to use async context managers and iterables from sync code
  • check out this article for why these are useful

Usage:

$ pip install asyncio-thread-runner
>>> async def double(i):
...     return i * 2
...
>>> from asyncio_thread_runner import ThreadRunner
>>> runner = ThreadRunner()
>>> runner.run(double(2))
4

Annotated example:

import aiohttp
from asyncio_thread_runner import ThreadRunner

# you can use ThreadRunner as a context manager,
# or call runner.close() when you're done with it
with ThreadRunner() as runner:

    # aiohttp.ClientSession() should be used as an async context manager,
    # enter_context() will exit the context on runner shutdown;
    # because instantiating ClientSession requires a running event loop,
    # we pass it as a factory instead of calling it in the main thread
    session = runner.enter_context(aiohttp.ClientSession)

    # session.get() returns an async context manager...
    request = session.get('https://death.andgravity.com/asyncio-bridge')
    # which we turn into a normal one with wrap_context()
    with runner.wrap_context(request) as response:

        # response.content is an async iterator;
        # we turn it into a normal iterator with wrap_iter()
        lines = list(runner.wrap_iter(response.content))

    # "got 935 lines"
    print('got', len(lines), 'lines')

Download files

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

Source Distribution

asyncio_thread_runner-1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

asyncio_thread_runner-1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_thread_runner-1.0.tar.gz.

File metadata

  • Download URL: asyncio_thread_runner-1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for asyncio_thread_runner-1.0.tar.gz
Algorithm Hash digest
SHA256 9b3f6c30d3ee8ae5cca1a243b6bb872404bdbcd88ec39fbbffa7984b96493182
MD5 72f86f28aee0b175c7d54613770cb0db
BLAKE2b-256 03fd1b5b3452f9e7df89b0c9bd246d276e0ae3a6b6bd7304076e09f2df1bf9fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncio_thread_runner-1.0.tar.gz:

Publisher: publish.yaml on lemon24/asyncio-thread-runner

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

File details

Details for the file asyncio_thread_runner-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_thread_runner-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7cc3f8b2f8446ac9450132ae8372a4810734ea8399e20d4233cc43920d8c812
MD5 8a2daaf32a187b4a72cde07ab44caf53
BLAKE2b-256 8d871fead469bb900643e2962aaf49b2b69f0e3222b652fd78d3a8df6fbb0e17

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncio_thread_runner-1.0-py3-none-any.whl:

Publisher: publish.yaml on lemon24/asyncio-thread-runner

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

Release history Release notifications | RSS feed

This release

1.0 This release

2 files

Supported by

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