Skip to main content

Thread-safe synchronization primitives

Project description

Asyncio ↔ Thread Synchronisation library

This library provides synchronisation primitives between asyncio and threads in Python. It allows you to coordinate the execution of asyncio coroutines and threads using familiar synchronisation primitives such as Event and Channel.

Installation

You can install this library using pip:

pip install threadsafe-async

Usage

Event

To use the Event class, import it from the library and create an instance:

from tsasync import Event

event = Event()

You can then use the wait(), set(), clear(), and is_set() methods of the Event instance to coordinate the execution of asyncio coroutines and threads. For example:

async def coroutine(event):
    await event.wait()
    # event is set

def thread(event):
    event.set()

You can use the wait() method synchronously from the thread

event.wait()

and asynchronously from the coroutine in event loop:

await event.wait()

The wait() method blocks until the event is set, while the set() method sets the event and wakes up all tasks waiting for it. The clear() method clears the event, causing tasks waiting for it to block again. The is_set() method returns True if the event is set, and False otherwise.

Note that you should only call the wait() method from the same thread or asyncio event loop that created the Event instance. If you try to call wait() from a different thread or event loop, a RuntimeError will be raised.

Channel

It allows you to send and receive data between asyncio coroutines and threads.

To use the Channel class, import it from the library and create an instance:

from tsasync import Channel

channel = Channel()

You can then use the send() and receive() methods of the Channel instance to communicate between asyncio coroutines and threads:

async def consumer_coroutine(channel):
   data = await channel.receive()
   ...

def consumer_thread(channel):
   data = channel.receive()
   ...

async def producer_coroutine(channel):
   ...
   await channel.send(data)

def producer_thread(channel):
   ...
   channel.send(data)

The send() method sends data to the channel, while the receive() method receives data from the channel. The send() method will block until the receive side of the channel is ready to receive data. The receive() method will block until there is data available to be received.

Note that the send() and receive() methods are will block the main thread if there are using without await.

If you want to use a buffered channel with a maximum size, you can pass the buffered argument to the Channel constructor:

channel = Channel(buffered=10)

This will create a channel with a buffer of 10 items. If the buffer is full, the send() method will block until there is room in the buffer.

License

This library is licensed under the MIT License. See the LICENSE file for more information.

Contributing

Contributions are welcome! Please open an issue or pull request on the GitHub repository if you have any suggestions or bug reports.

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

threadsafe-async-0.2.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

threadsafe_async-0.2.1-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file threadsafe-async-0.2.1.tar.gz.

File metadata

  • Download URL: threadsafe-async-0.2.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for threadsafe-async-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7cbfeb623fd56c8e5bd09da624ece346c8f34c303ddd8cb6a23c041a443c453a
MD5 362c9f357c02747fa9133e86bfddbf55
BLAKE2b-256 22b1db6e3ed2b51410328d8e3b3669325aae620c8dd4334ab895a5bd27f26bf2

See more details on using hashes here.

File details

Details for the file threadsafe_async-0.2.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for threadsafe_async-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0b2660b10d4f3a89e4ea386512f2ee2de1b655523124910b45e8b3af7c0c858f
MD5 6f6deb42ccdaed070cd941e5f5512e92
BLAKE2b-256 5f1e7e5e59cd4853b0a86c99ebf033df1678e0182c26ac0648e5dc3115e3249c

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