Skip to main content

Python Thread Manager

Project description

thread-manager-py

Python Thread Manager

✅Build And Test ✅ PyPI version PyPI
CodeFactor PyPI

Install

pip install thread-manager-py

Pool Manager Usage

You can find examples in test/test_pool_manager.py.

Simple Usage

import os
from pool_manager import PoolManager

def calculate(x):
    print(f"[{os.getpid()}]  func: {x}\t\t", r := x ** 5 ** 2, flush=True)
    return r

manager = PoolManager()
manager.add_task(calculate, [i for i in range(2, 22)])
manager.run_map()

manager.add_task(calculate, [i for i in range(2, 22)])
manager.add_task(calculate, [i for i in range(2, 22)])
manager.run_map()

task_result = manager.get_task_result()
with PoolManager() as manager:
    manager.add_task(calculate, [i for i in range(2, 22)])
    manager.add_task(calculate, [i for i in range(2, 22)])
    manager.add_task(calculate, [i for i in range(2, 22)])
    manager.run_map()

Thread Manager Usage

You can find examples in test/test_thread_manager.py.

Simple Usage

import time
from thread_manager import ThreadManager, ThreadArgument

def print_something(name: str, number: int):
    print(name, number)
    time.sleep(1)

    
thread_manager = ThreadManager(print_something, [
    ThreadArgument(thread_name=f"Thread:{x}", args=(x, x) )
    for x in range(1, 23)
])
thread_manager.run()

Get Thread Error

errors = thread_manager.get_errors()
has_error = thread_manager.has_error()
error_count = thread_manager.get_error_count()

for e in errors:
    print(e)

Simple Usage with decorator

from thread_manager import using_thread

@using_thread
def print_something(number, **kwargs):
    print(number, kwargs)

for i in range(10):
    print_something(i, name=f"thread-{i}")

Simple Usage with Exception Hook

from thread_manager import ThreadManager, ThreadArgument

errors = []

def func_something(*args):
    raise Exception("test error")

def func_exception_hook(*args):
    errors.append(args)

thread_manager = ThreadManager(func_something, [
    ThreadArgument(thread_name=f"Thread:{x}", args=(x, x), kwargs={}, )
    for x in range(1, 23)
], except_hook=func_exception_hook)
thread_manager.run()

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

thread-manager-py-0.3.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

thread_manager_py-0.3.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file thread-manager-py-0.3.0.tar.gz.

File metadata

  • Download URL: thread-manager-py-0.3.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for thread-manager-py-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f1d06f93ebbbfedcca27a67841371d9a1e0107a717973bef50e828c9004e68f1
MD5 6291b091e8df3f181eba60ccd2cedf3e
BLAKE2b-256 69ab83c9913bea2dcdc7e31489437221b29fcfc8ed08afe9d5fa106e00e8e63a

See more details on using hashes here.

File details

Details for the file thread_manager_py-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for thread_manager_py-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 993d2f57aecfaa583fb5956a4326def9d4d7e7ed009efd955003959849eb7b25
MD5 1373cf81e52fd91e6fa83c2b778d9c07
BLAKE2b-256 902f33477c85da848211cc89221cef05b6299f56b8c8794d02b15caa6f5366b5

See more details on using hashes here.

Supported by

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