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

TPool offers two distinct types of thread pools to cater to different use cases:

SeqPool

SeqPool is designed for scenarios where you have a predefined set of inputs and wish to run the same function (target) across multiple threads. It ensures that no more than a specified number (pool_size) of threads are running concurrently.

  • Use Case: Ideal for batch processing tasks where each task can run in parallel, but the order of execution matters.
  • Execution: The pool executes the provided function with different arguments, managing the concurrency to avoid overwhelming system resources.

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.

Detailed Description of WildPool:

  • Concurrency Management: WildPool manages a fixed number of threads, ensuring no more than the specified number of tasks run concurrently, which helps in maintaining optimal system performance.
  • Non-Sequential Execution: Tasks are executed as soon as resources are available, without guaranteeing any particular order, making it ideal for tasks that do not depend on each other.
  • Automatic Cleanup: Completed threads are automatically removed from the pool, freeing up resources for new tasks.
  • Daemon-Friendly: The pool is well-suited for daemon processes and long-running services where tasks need to be processed continuously in the background.

Examples

Example 1: Wild Pool

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()
        val = ""
        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.join()
        print(self.data)


a = Example()
a.test_threads()

Example 2: Sequential Pool

from multiprocessing import Process, Lock, Pipe
from TPool import SeqPool
import string


class Example:

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

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

    def test_threads(self):
        params_list = []
        lock = Lock()
        for i in range(100):
            params_list.append((i, lock))
        pool = SeqPool(pool_size=10, target=self.set_even_or_odd, params_list=params_list)
        pool.run()
        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.6.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

TPool-1.6-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tpool-1.6.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for tpool-1.6.tar.gz
Algorithm Hash digest
SHA256 354a02f877879607fbff09e57ad1b8b2701a97c6e4e3a1b07a3c2a6f40e652a2
MD5 5e8a668b4dcaadd4da421fceb2a2791c
BLAKE2b-256 fd9efaa957d695f731cea3fe3af26b467ea232bb12b99982700f37116124876a

See more details on using hashes here.

File details

Details for the file TPool-1.6-py3-none-any.whl.

File metadata

  • Download URL: TPool-1.6-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for TPool-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a484005b6f9a3a6cae7cd96bd142c11869151987a6e419096e34c5377b1f2451
MD5 6fbb691b7fb0de7f29dcf6df62430e5c
BLAKE2b-256 e571c38f0d3a0ff79a5f7bed3967483f207370abe0df33cf09e0428c4ba83201

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