Skip to main content

A Python library for executing tasks in parallel with threads and queues

Project description

Executor

Fast execute task with python threading and efficient mem ops

Package version Supported Python versions

Installation

pip install thread-executor

Why do we need Thread Executor?

Python threading module is a great structure, it helps developers to folk a thread to run some background tasks. Python have Queue mechanism to connect thread's data. But, what is the problem??

  • First, threading module create threads but number of threads that can be created is depended on the hardware. So there is a limit number of threads that can be created. It's fast and lightweight with small traffic but when server is high load you will have some problem, high pressure on memory because you can't create too many threads. can't create more threads

  • Second, when you create and release threads many times, it'll increase memory and CPUs time of system. Sometime, developers did not handle exceptions and release thread cause thread leak(memory leak). It can put more pressure on the application. waste of resource

How to resolve problem??

This's my resolver.

  • We create exact or dynamic number of threads. Then using Job - a unit bring data information to Worker to process. Workers don't need to release, and you only create 1 time or reset it when you update config.

  • Job brings 2 importance fields: func and args and you can call them by func(*args) and get all the results and return on callback is optional.

  • Your app doesn't need to create and release threads continuously

  • Easy to access and use when coding.

Disadvantage?

  • If you use callback then remembered to add try catch to handle thread leaked.
  • If queue is full you need to wait for available queue slot. set max_queue_size=0 to avoid this.
  • If you restart your app, all the Job in Queue that have not been processed will be lost.

Usage : Interface list

send(job: Job) -> None # Push a job to the queue
wait() -> None # wait for all jobs to be completed without blocking each other
scale_up(number_threads: int) -> None # scale up number of threads
scale_down(self, number_threads: int) -> None # scale down number of threads

Initial

from executor.safe_queue import Executor, Job

engine = Executor(number_threads=10, max_queue_size=0)

Send Simple Job

import time

def test_exec(*args, **kwargs):
    time.sleep(3)
    print(args)
    return [1, 2, 3]


def test_exec1(*args, **kwargs):
    print(kwargs)
    time.sleep(2)
    return {"a": 1, "b": 2, "c": 3}

engine.send(Job(func=test_exec, args=(1, 2), kwargs={}, callback=None, block=False))
engine.send(Job(func=test_exec1, args=(), kwargs={"time": 1}, callback=None, block=False))
engine.send(Job(func=test_exec1, args=(), kwargs={}, callback=None, block=False))
engine.send(Job(func=test_exec1, args=(), kwargs={}, callback=None, block=False))
engine.send(Job(func=test_exec1, args=(), kwargs={}, callback=None, block=False))
engine.wait()

Send Job with callback

def call_back(result):
    print(result)
    
for i in range(5):
    engine.send(Job(func=test_exec1, args=(), kwargs={"time": 1}, callback=call_back, block=False))
engine.wait()

Thread scale up/down

engine.scale_up(3)
engine.scale_down(3)

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_executor-0.1.3.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

thread_executor-0.1.3-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file thread_executor-0.1.3.tar.gz.

File metadata

  • Download URL: thread_executor-0.1.3.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.5 Windows/10

File hashes

Hashes for thread_executor-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9604efd25c49a4850a0a3a304fec0d5abf7765283115e266064b3f9d8c77f326
MD5 d7eb9eae173f643f0c5875b93b07adc6
BLAKE2b-256 beaffab10d1a8d5d8d0a62187588fb9778841452f7b42cba68897a5c7d0ca074

See more details on using hashes here.

File details

Details for the file thread_executor-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: thread_executor-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.5 Windows/10

File hashes

Hashes for thread_executor-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d5347ef2dfbc7a719eacd8d967651960ebd7ca8f6019ba5f71a5b0d74211d3b3
MD5 b7c7e8f5b593fa89717492c41f6058e9
BLAKE2b-256 3593a2ff62c6d5dd6ed6b58d456e5ffb6b82a5ce38472f52a3f3f8393a31e11c

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