This release marks a significant evolution of ThreadFactory, introducing a modular and powerful concurrency stack built from first principles. The entire system has been refactored into high-performance primitives, orchestrators, dispatchers, sync types, and thread tools, providing granular control and unprecedented performance for No-GIL Python applications.
๐ What's New
๐ Sync Types โ concurrency.sync_types
Thread-safe wrappers for Pythonโs core data types, designed for deterministic, low-contention concurrent access. These are now reference types and should be used with care.
SyncInt
: Atomic integer with full arithmetic and bitwise support.SyncBool
: Thread-safe boolean with complete logical operation support.SyncString
: A mutable, thread-safe wrapper for strings.SyncFloat
: Atomic float with full arithmetic support.SyncRef
: An atomic, thread-safe reference to any object, enabling safe read/write access and conditional updates.
๐ง New Primitives โ synchronization.primitives
A suite of low-level building blocks for sophisticated synchronization patterns.
Dynaphore
: A dynamically resizable permit gate, ideal for adaptive queues and elastic thread pools.FlowRegulator
: A smart semaphore with factory ID targeting and callback routing, perfect for agentic workers.SmartCondition
: An advancedCondition
alternative that allows targeted wakeups and ULID tracking.TransitCondition
: A minimalist and lightweight wait/notify condition with FIFO-safe callbacks.SignalLatch
: A latch with observer signaling that can notify a controller before blocking.Latch
: A classic, reusable latch that releases all threads permanently once opened.
โก New Coordinators โ synchronization.orchestrators
High-level tools for managing complex thread workflows.
TransitBarrier
: A reusable barrier with threshold coordination and an optional callable execution.SignalBarrier
: A signal-based barrier supporting thresholds, timeouts, and failure states.ClockBarrier
: A time-sensitive barrier that fails if threads do not arrive before a global timeout.Conductor
: A reusable group synchronizer that executes tasks after a thread threshold is met.MultiConductor
: Manages multiple groups with per-group tasks and a global thread threshold, supporting lock-step, forked, and synchronized forked execution.Scout
: A predicate-based monitor that blocks one thread while evaluating a condition with a timeout.
๐ New Execution Gates โ synchronization.execution
BypassConductor
: Allows a fixed number of threads (N
) to execute a callable pipeline, capturing results via anOutcome
. It's perfect for controlled bootstraps or one-time initializers.
๐ New Dispatchers โ synchronization.dispatchers
Advanced tools for routing threads to callables.
Fork
: Assigns callables based on usage caps, ensuring each executes a fixed number of times.SignalFork
: Routes threads to callables immediately on arrival and notifies a controller when all slots are consumed.SyncFork
: CoordinatesN
threads into callable groups that execute simultaneously once all slots are filled.SyncSignalFork
: ExtendsSyncFork
with the ability to execute a callable as a signal.
๐ฎ New Controller โ synchronization.controller
SignalController
: A central registry for managing the lifecycle of concurrency objects. It supports registration, invocation with hooks, event notification, and a thread-safedispose()
method for recursively tearing down all managed objects.
โฑ๏ธ New Utilities
AutoResetTimer
(thread_factory.utils.timing
): A timer that automatically resets, useful for cyclic backoffs and heartbeats.Stopwatch
(thread_factory.utils.timing
): A simple nanosecond-precision profiler for performance tracking.Package
(thread_factory.utils.coordination.package
): A thread-safe, delegate-style wrapper for callables.
โ Structural and Performance Improvements
__slots__
Adoption: All concurrency classes now use__slots__
, resulting in a reduced memory footprint, faster attribute access, and less garbage collector churn under high load.- New Folder Structure: The
synchronization
module is now organized by purpose, improving clarity and maintainability:primitives/
: Low-level synchronization building blocks.orchestrators/
: Group coordination and flow control.dispatchers/
: Thread-callable routing logic.execution/
: Execution gates and work-limited runners.controller/
: Lifecycle and command management.
- Queue and Stack Enhancements:
ConcurrentQueue
andConcurrentStack
now feature anis_empty()
method for graceful shutdown checks andbatch_steal()
for optimized consumer loops.
โ Important Changes & Renaming
To improve clarity and consistency across the new modular architecture, the following classes have been renamed:
ActionBarrier
is nowTransitBarrier
.SignalCondition
is nowTransitCondition
.
๐ Developer Notes
- For modern worker-oriented designs, prefer using
FlowRegulator
combined withSmartCondition
. - When implementing fork-like behaviors, use the new
Fork
orSyncFork
dispatchers. - For performance instrumentation, adopt
Stopwatch
andAutoResetTimer
. - Use
SignalCondition
for simple signaling needs andSmartCondition
for targeted, complex scenarios. - Leverage
ConcurrentQueue.is_empty()
to manage graceful shutdowns in your worker threads.
Thank you for using ThreadFactory! We believe this massive upgrade will unlock new levels of performance and control in your concurrent applications.
Full Changelog: v1.2.4...v1.5.0