Skip to main content

A synchronization primitive containing a value, allowing threads to concurrently set the value and peek the most recently set value.

Project description

messageboard

A synchronization primitive containing a value, allowing threads to concurrently set the value and peek the most recently set value.

Features

  • Thread-safe.
  • .set(value) sets the value.
  • .peek() peeks the most recently set value. If no value is set yet, .peek() blocks until a value is set.

Usage

from __future__ import print_function
import random

random.seed(42)
import threading
import time

from messageboard import MessageBoard


def writer(message_board):
    for i in range(10):
        message_board.set(i)
        print('Written', i)
        time.sleep(1)


def reader(sticky_value):
    for i in range(10):
        if random.random() > 0.5:
            sticky_value.peek()
            print('Peeked', i)
        time.sleep(1)


board = MessageBoard()

writer_thread = threading.Thread(target=writer, args=(board,))
writer_thread.daemon = True

reader_thread = threading.Thread(target=reader, args=(board,))
reader_thread.daemon = True

writer_thread.start()
reader_thread.start()

writer_thread.join()
reader_thread.join()

This produces the following output:

Written 0
Peeked 0
Written 1
Written 2
Written 3
Written 4
Peeked 4
Written 5
Peeked 5
Written 6
Peeked 6
Written 7
Written 8
Written 9

Contributing

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

License

This project is licensed under the MIT 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

messageboard-0.1.0a0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

messageboard-0.1.0a0-py2.py3-none-any.whl (3.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for messageboard-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 aba86f283d86e7afab984f85a59eedb233820309f3ce08582244bca24021693a
MD5 723165512ff1a830318a9708de365392
BLAKE2b-256 a50cd712dbaa7b1b15752f7aa2f238cc3654cf5d02bc2f7ab468849f1b870979

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for messageboard-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4b259a7fcbde00f978e95c7446e49d07396ea5ef993aea868575e3fe11db7872
MD5 495eaecf59704c9a90ef87fc3ce9bbac
BLAKE2b-256 2c8c4d2a0f0ab71ce0bc15f96038b9f7571e409fded01e519644762e1d5c3732

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