Skip to main content

newt

Thread-safe, mixed-threading-and-asyncio, producer-consumer queue for Python.

Heavily based on janus, but newt lazily initializes event loop which makes the queue much more flexible:

  • newt.Queue could be initialized outside subthread or coroutine
  • supports information exchange between a thread and a coroutine
  • ensures thread-safety

Install

$ pip install newt

Usage

Suppose there is a threaded target function which produces items, and a coroutine which consumes items.

from newt import Queue


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

sync_queue follows the interface of Python built-in synchronized queue class

async def coroutine(async_queue):
    for i in range(100):
        assert await async_queue.get() == i
        async_queue.task_done()

async_queue follows the vanilla Python asyncio.Queue

Thread in an executor -> Coroutine

The following example shows how to produce item in a thread which executed in the executor, and consume the item in a coroutine.

import asyncio

loop = asyncio.get_event_loop()


async def main():
    future = loop.run_in_executor(None, threaded, queue.sync_queue)
    await coroutine(queue.async_queue)
    await future

    queue.close()
    await queue.wait_closed()

loop.run_until_complete(main())

Normal thread -> Coroutine

newt.Queue also supports to produce item in a normal threading,

loop = asyncio.get_event_loop()


async def main():
    await coroutine(queue.async_queue)
    queue.close()
    await queue.wait_closed()

t = threading.Thread(target=threaded, args=(queue.sync_queue,))
t.start()

loop.run_until_complete(main())

License

MIT

Metadata

Release files for newt 0.1.0

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

Source distribution (sdist)

Source distribution for newt 0.1.0
File Size Uploaded Provenance
newt-0.1.0.tar.gz 9.3 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 18.8 kB

Release files / newt-0.1.0.tar.gz

Download URL newt-0.1.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ee184e66764b9ec3f6fae548f30d5c73a33a7b213a7058523623967d1ce55c64
BLAKE2b-256 checksum
How to use checksums
82cf64f2db5fa2c218ddf3614d5298a5a2a2c7a58091ccd1f4c29f0fcd6888db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release files / newt-0.1.0-py3-none-any.whl

Download URL newt-0.1.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76910335ef3daef346e694e428e88f26bb64f0f85c3907204c02f3121b088ec7
BLAKE2b-256 checksum
How to use checksums
2aa0d3b2c47ad22713d5772cc45ee184517a2adee78794e00524b47485d64c85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Security for newt 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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