Skip to main content

Mixed sync-async queue to interoperate between asyncio tasks and classic threads

Project description

https://github.com/aio-libs/janus/actions/workflows/ci.yml/badge.svg https://codecov.io/gh/aio-libs/janus/branch/master/graph/badge.svg https://img.shields.io/pypi/v/janus.svg Chat on Gitter

Mixed sync-async queue, supposed to be used for communicating between classic synchronous (threaded) code and asynchronous (in terms of asyncio) one.

Like Janus god the queue object from the library has two faces: synchronous and asynchronous interface.

Synchronous is fully compatible with standard queue, asynchronous one follows asyncio queue design.

Usage

Three queues are available:

  • Queue

  • LifoQueue

  • PriorityQueue

Each has two properties: sync_q and async_q.

Use the first to get synchronous interface and the second to get asynchronous one.

Example

import asyncio
import janus


def threaded(sync_q: janus.SyncQueue[int]) -> None:
    for i in range(100):
        sync_q.put(i)
    sync_q.join()


async def async_coro(async_q: janus.AsyncQueue[int]) -> None:
    for i in range(100):
        val = await async_q.get()
        assert val == i
        async_q.task_done()


async def main() -> None:
    queue: janus.Queue[int] = janus.Queue()
    loop = asyncio.get_running_loop()
    fut = loop.run_in_executor(None, threaded, queue.sync_q)
    await async_coro(queue.async_q)
    await fut
    await queue.aclose()


asyncio.run(main())

Limitations

This library is built using a classic thread-safe design. The design is time-tested, but has some limitations.

  • Once you are done working with a queue, you must properly close it using aclose(). This is because this library creates new tasks to notify other threads. If you do not properly close the queue, asyncio may generate error messages.

  • The library has quite good performance only when used as intended, that is, for communication between synchronous code and asynchronous one. For sync-only and async-only cases, use queues from queue and asyncio queue modules, otherwise the slowdown can be significant.

  • You cannot use queues for communicating between two different event loops because, like all asyncio primitives, they bind to the current one.

Development status is production/stable. The janus library is maintained to support the latest versions of Python and fixes, but no major changes will be made. If your application is performance-sensitive, or if you need any new features such as anyio support, try the experimental culsans library as an alternative.

Communication channels

GitHub Discussions: https://github.com/aio-libs/janus/discussions

Feel free to post your questions and ideas here.

gitter chat https://gitter.im/aio-libs/Lobby

License

janus library is offered under Apache 2 license.

Thanks

The library development is sponsored by DataRobot (https://datarobot.com)

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

janus-2.0.0.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

janus-2.0.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file janus-2.0.0.tar.gz.

File metadata

  • Download URL: janus-2.0.0.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for janus-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0970f38e0e725400496c834a368a67ee551dc3b5ad0a257e132f5b46f2e77770
MD5 7c8ec257be4e90abc47a7df47df88436
BLAKE2b-256 d87f69884b6618be4baf6ebcacc716ee8680a842428a19f403db6d1c0bb990aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for janus-2.0.0.tar.gz:

Publisher: ci.yml on aio-libs/janus

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

File details

Details for the file janus-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: janus-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for janus-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e6449d34eab04cd016befbd7d8c0d8acaaaab67cb59e076a69149f9031745f9
MD5 5f2932b1e695451d8b7d7537bbeaf846
BLAKE2b-256 683465604740edcb20e1bda6a890348ed7d282e7dd23aa00401cbe36fd0edbd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for janus-2.0.0-py3-none-any.whl:

Publisher: ci.yml on aio-libs/janus

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

Supported by

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