Skip to content

Python classes for safe deployment and management of Threads, Processes, synchronization primitives (Locks, Barriers, etc), thread-safe, and process-safe Python data structures (lists, dicts, sets, etc).

License

Notifications You must be signed in to change notification settings

andre-romano/safethread

Repository files navigation

safethread

PyPI License Downloads

safethread is a Python package that wraps common Python data structures in thread-safe / multiprocess-safe classes, providing utilities for thread-safe and multiprocess-safe operations and synchronization mechanisms (i.e., shared resources access and inter-process communication). It includes custom data structures and processing classes designed to ensure thread safety when used in multi-threaded programming, and multiprocessing safety when used in multi-processing programs.

Features

  • Thread-Safe and Multiprocessing-Safe Data Structures: SafeProcessList, SafeProcessDict, ThreadSafeList, ThreadSafeDict, SafeThreadSet, among others.
  • Thread and Inter-process Synchronization: Built-in locking mechanisms to ensure safe operations in multithreaded and multiprocessing environments.
  • Threaded and Multiprocessing Classes: Threaded and multiprocessing classes to perform parallel data processing (Pipeline class), scheduled function calls (Scheduler class), among others.
  • Utility Classes and Functions: Additional helpers and utilities for threading (Pipeline, PipelineStageThreaded, SubprocessThreaded , Publish/Subscribe, etc), for multiprocessing (Pipeline, PipelineStageProcessed, SubprocessProcessed etc), thread synchronization, and inter-process synchronization (IPC).

Installation

You can install safethread from PyPI:

pip install safethread

Usage

from safethread.thread.datatype import ThreadSafeList, ThreadSafeDict
from safethread.thread.utils import PipelineStageThreaded

# Using ThreadSafeList
safe_list = ThreadSafeList()
safe_list.append(1)
print(safe_list[0])  # Output: 1

# Using ThreadSafeDict
safe_dict = ThreadSafeDict()
safe_dict['key'] = 'value'
print(safe_dict['key'])  # Output: 'value'

# Using Pipeline (separate working thread)
stage = PipelineStageThreaded(lambda x: x * 2)
stage.start()

# Put some values into the pipeline for processing
stage.put(5)
stage.put(10)

# Get and print the results
print(f"Processed result 1: {stage.get()}")  # Output: 10 (5 * 2)
print(f"Processed result 2: {stage.get()}")  # Output: 20 (10 * 2)

# Stop pipeline
stage.stop()
stage.join()

For further details, check the examples/ folder and the full documentation (link below).

Documentation

The full documentation is available in https://andre-romano.github.io/safethread/docs

Contributing

We welcome contributions! If you'd like to contribute, please fork the repository and submit a pull request.

Special thanks / Acknowledgments

  • pdocs
  • PyPi
  • Python 3

License and Copyright

Copyright (C) 2025 - Andre Luiz Romano Madureira

This project is licensed under the Apache License 2.0.

For more details, see the full license text (see LICENSE file).

About

Python classes for safe deployment and management of Threads, Processes, synchronization primitives (Locks, Barriers, etc), thread-safe, and process-safe Python data structures (lists, dicts, sets, etc).

Topics

Resources

License

Stars

Watchers

Forks

Languages