Skip to main content

A thread synchronization primitive for Python like `threading.Event`, but instead of just a boolean flag, it carries any Python value.

Project description

ThreadingValueEvent

A thread synchronization primitive for Python like threading.Event, but instead of just a boolean flag, it carries any Python value. Threads can wait for a value to be set, retrieve it, and optionally reset the event for reuse.

Features

  • Wait for an event to be set, optionally with a timeout.
  • Pass any Python object (not just a boolean) from one thread to another.
  • Ability to clear/reset the event.
  • Intuitive, event-like API.

Installation

pip install threading-value-event

Usage

from __future__ import print_function
from threading_value_event import ThreadingValueEvent
import threading
import time

event = ThreadingValueEvent()


def worker():
    print('Worker waiting for value...')
    result = event.wait()
    print('Worker got:', result)


thread = threading.Thread(target=worker)
thread.start()

time.sleep(1)
print('Main thread setting value to 123.')
event.set(123)
thread.join()

Output:

Worker waiting for value...
Main thread setting value to 123.
Worker got: 123

API

ThreadingValueEvent()

Create a new event object.

set(value)

Set the event, storing value. Wakes up all threads waiting for this event.

is_set()

Return True if the event is set, else False.

wait(timeout=None, default=None)

Block until the event is set, then return the value. If a timeout is provided and the event isn't set in time, returns default.

clear()

Clear the event and remove the stored value.

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the License.

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

threading_value_event-0.1.0a0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

threading_value_event-0.1.0a0-py2.py3-none-any.whl (3.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file threading_value_event-0.1.0a0.tar.gz.

File metadata

  • Download URL: threading_value_event-0.1.0a0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for threading_value_event-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 651d3764282af37370324c9c8dad28258198163db2e6c87c0cb90f4ae92b18dd
MD5 5cbd639803ef0800586b6a6d231c7570
BLAKE2b-256 9edc2b685fd341f61e4d81d060be22b5d75e90273bf54908c560a570aed21144

See more details on using hashes here.

File details

Details for the file threading_value_event-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for threading_value_event-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fa26428a9d91a1792af1f9b00ce981c825b48027028491c847655707010aff6b
MD5 cb7c9fa5535878fbf8b58edc05cf9796
BLAKE2b-256 d42782ad4d113e9b26332e599b1a0a1b266b959cadb1d2a80073011e379573e7

See more details on using hashes here.

Supported by

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