Skip to main content

Pool for Threads

Project description

TPool

TPool is a Python 3 library that provides flexible and powerful thread pools designed for handling concurrent tasks with ease. It supports the execution of multiple threads with various configurations and allows for both sequential and non-sequential task management. The documentation is available here

Install

pip install TPool

Run Tests

python -m unittest discover

Thread Pools Overview

WildPool is the core component of TPool — a robust thread pool implementation that efficiently manages concurrent execution of independent tasks.

WildPool

WildPool is a flexible thread pool designed for managing concurrent tasks in non-sequential order. It efficiently handles multiple threads, allowing different functions to run concurrently without guaranteeing the order of execution.

  • Use Case: Perfect for handling diverse tasks in daemon processes (e.g., messaging queues), where tasks need to be processed as soon as resources are available.
  • Execution: A worker thread manages the execution of threads, ensuring that the number of concurrent threads does not exceed the specified pool_size. Threads are automatically removed from the pool upon completion.

Key Features

  • Concurrency Management: Ensures that no more than a specified number of threads run concurrently (pool_size).
  • Non-Sequential Execution: Tasks are executed as soon as resources are available — task order is not guaranteed.
  • Automatic Cleanup: Threads are automatically removed from the pool after completion.
  • Daemon-Friendly: Well-suited for background services and long-running daemons.
  • Graceful Shutdown: Use .join() to wait for all tasks or .stop() to shut down manually.

Example: Using WildPool

from TPool import WildPool
from threading import Thread, Lock
import string
import logging


class Example:

    def __init__(self):
        self.data = dict()
        self.lock = Lock()

    def set_even_or_odd(self, n):
        print(f"processing: {n}")
        self.lock.acquire()
        if n%2 == 0:
            val = "even"
        else:
            val = "odd"
        self.data[n] = val
        self.lock.release()

    def test_threads(self):
        logger = logging.getLogger('WildPool')
        logger.setLevel(logging.DEBUG)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        logger.addHandler(ch)
        pool = WildPool(pool_size=5, logger=logger)
        for i in range(100):
            th = Thread(target=self.set_even_or_odd, args=(i,))
            pool.add_thread(th)
        pool.start_worker()
        pool.join()
        print(self.data)


a = Example()
a.test_threads()

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your improvements or bug fixes.

License

This project is licensed under the Apache License. See the LICENSE file for details.

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

tpool-1.7.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

tpool-1.7-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file tpool-1.7.tar.gz.

File metadata

  • Download URL: tpool-1.7.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for tpool-1.7.tar.gz
Algorithm Hash digest
SHA256 898d06d1e902d6a7e66e6cbcdee8660ed7c1b695cb0bc1a5a789b1b43054d0b4
MD5 4bde678fe5eb48a2bf086d20b206563c
BLAKE2b-256 b400086084c23217745b90b7e787f8925a52981585e49766546baf75bc8a42df

See more details on using hashes here.

File details

Details for the file tpool-1.7-py3-none-any.whl.

File metadata

  • Download URL: tpool-1.7-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for tpool-1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 442fa2cd569ec1100ac67155b96d84eb900959ce65a9bbd31334ebc62a2285a5
MD5 49a52e5fef5989f74b2a2e766a008892
BLAKE2b-256 5639d4b33de4aa616d48ee1472ee55b787149af832d6ce348dc0871f463fad5c

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