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 example (Python 3.7+)

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
    queue.close()
    await queue.wait_closed()


asyncio.run(main())

Usage example (Python 3.5 and 3.6)

import asyncio
import janus

loop = asyncio.get_event_loop()


def threaded(sync_q):
    for i in range(100):
        sync_q.put(i)
    sync_q.join()


async def async_coro(async_q):
    for i in range(100):
        val = await async_q.get()
        assert val == i
        async_q.task_done()


async def main():
    queue = janus.Queue()
    fut = loop.run_in_executor(None, threaded, queue.sync_q)
    await async_coro(queue.async_q)
    await fut
    queue.close()
    await queue.wait_closed()

try:
    loop.run_until_complete(main())
finally:
    loop.close()

Communication channels

aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs

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-0.7.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

janus-0.7.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: janus-0.7.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for janus-0.7.0.tar.gz
Algorithm Hash digest
SHA256 f10dcf5776e8d49cc30ec86d5eb7268eeec39abaa24fe0332ee8fb8fa3611845
MD5 4618dd53d5cb870e029bfaf3d2823ace
BLAKE2b-256 91dd6e74ca2668c4a7360e5cea982e0da2d9fedca5b403327ad622aa3509e1a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: janus-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for janus-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41e267f50b10f981c1d29f9fd065ec27086d32f823cefb39808ba7887b39f860
MD5 bab9e950cdd8895e337d3a3f54fdab52
BLAKE2b-256 57f891caa9e4ee5e1cdd1d8dccad11e851634ba44a274e391d27b0a8655b81f7

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