Skip to main content

A lightweight framework for running functions concurrently across multiple threads while maintaining a defined execution order.

Project description

ci PyPI version

threaded-order

A lightweight Python framework for running functions concurrently across multiple threads while maintaining defined execution order. It lets you declare dependencies between tasks—so some run only after others complete—without complex orchestration code.

Ideal for dependency-aware test execution, build pipelines, and automation workflows that benefit from controlled concurrency.

Key Features

  • Concurrent task execution using Python threads
  • Dependency graph automatically determines order
  • Simple registration and decorator API
  • Thread-safe logging, callbacks, and run summary
  • Graceful shutdown on interrupt

Installation

pip install threaded-order

Simple Example

from threaded_order import Scheduler, ThreadProxyLogger
from time import sleep

s = Scheduler(workers=3, setup_logging=True)
logger = ThreadProxyLogger()

@s.dregister()
def a(): sleep(1); logger.info("a")

@s.dregister(after=['a'])
def b(): sleep(1); logger.info("b")

@s.dregister(after=['a'])
def c(): sleep(1); logger.info("c")

@s.dregister(after=['b', 'c'])
def d(): sleep(1); logger.info("d")

if __name__ == '__main__':
    s.on_scheduler_done(lambda s: print(f"Passed:{len(s['passed'])} Failed:{len(s['failed'])}"))
    s.start()

Output:

2025-11-11 22:07:33 [MainThread]: starting thread pool with 3 threads
2025-11-11 22:07:34 [thread_0]: a
2025-11-11 22:07:35 [thread_1]: c
2025-11-11 22:07:35 [thread_0]: b
2025-11-11 22:07:36 [thread_1]: d
2025-11-11 22:07:36 [MainThread]: all work completed
2025-11-11 22:07:36 [MainThread]: duration: 3.01s
Passed:4 Failed:0

ProgressBar Integration

Can be done by using the on_task_done callback. See example3b

example1

See examples in examples folder. To run examples, follow instructions below to build and run the Docker container then execute:

API Overview

class Scheduler(workers=None, setup_logging=False, add_stream_handler=True)

Runs registered callables across multiple threads while respecting declared dependencies.

Core Methods

Method Description
register(obj, name, after=None) Register a callable for execution. after defines dependencies by name.
dregister(after=None) Decorator variant of register() for inline task definitions.
start() Start execution, respecting dependencies. Returns a summary dictionary.

Callbacks

All are optional and run on the scheduler thread (never worker threads).

Callback When Fired Signature
on_task_start(fn) Before a task starts (name)
on_task_done(fn) After a task finishes (name, ok)
on_scheduler_start(fn) Before scheduler starts running tasks (meta)
on_scheduler_done(fn) After all tasks complete (summary)

Interrupt Handling

Press Ctrl-C during execution to gracefully cancel outstanding work:

  • Running tasks finish naturally or are marked as cancelled
  • Remaining queued tasks are discarded
  • Final summary reflects all results

Development

Clone the repository and ensure the latest version of Docker is installed on your development server.

Build the Docker image:

docker image build \
-t threaded-order:latest .

Run the Docker container:

docker container run \
--rm \
-it \
-v $PWD:/code \
threaded-order:latest \
bash

Execute the dev pipeline:

make dev

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

threaded_order-1.3.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

threaded_order-1.3.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file threaded_order-1.3.0.tar.gz.

File metadata

  • Download URL: threaded_order-1.3.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for threaded_order-1.3.0.tar.gz
Algorithm Hash digest
SHA256 2c9c3fe775844fa1e6b9a7923768fffa0af2a7823cd731c20db930e8e88320b9
MD5 88dcfeebdb538036aa963e0e85004ecb
BLAKE2b-256 0074fb2e7f604404705e52a0874465de20d1eaf4b28dc7697567b2e4534d154e

See more details on using hashes here.

File details

Details for the file threaded_order-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: threaded_order-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for threaded_order-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba3fbe93074cbc2f3d29fe09439a5ea161ae55adb7b4847d5b2cb6ce816f9d3d
MD5 ba604411e8e2214005cf85ee51126ed0
BLAKE2b-256 ef3639b641b0d0f0a726b22dee7d658976264f58aad04690292cf9cb3d2dabb0

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