Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

worker-dispatcher

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

worker-dispatcher

A flexible task dispatcher for Python with multiple threading or processing control

  • 1.0.7
  • PyPI
  • Socket score

Maintainers
1

Python Worker Dispatcher


A flexible task dispatcher for Python with multiple threading or processing control

PyPI

Features

  • Tasks Dispatching to managed workers

  • Elegant Interface for setup and use

  • Various modes to choose from


OUTLINE


DEMONSTRATION

Just write your own callback functions using the library, then run it and collect the result details:

$ python3 main.py

Worker Dispatcher Configutation:
- Local CPU core: 10
- Tasks Count: 100
- Runtime: Unlimited
- Dispatch Mode: Fixed Workers (Default)
- Workers Info:
  └ Worker Type: Processing
  └ Number of Workers : 10
  └ Max Worker: 10

--- Start to dispatch workers at 2024-06-14T17:46:30.996685+08:00 ---

...(User-defined output)...

--- End of worker dispatch at 2024-06-14T17:46:41.420888+08:00---

Spend Time: 10.424203 sec
Completed Tasks Count: 100
Uncompleted Tasks Count: 0
Undispatched Tasks Count: 0

Use 20 theads concurrently to dispatch tasks for HTTP reqeusts

import worker_dispatcher
import requests

def each_task(id: int, config, task, log):
    response = requests.get(config['my_endpoint'] + task)
    return response

responses = worker_dispatcher.start({
    'task': {
        'list': ['ORD_AH001', 'ORD_KL502', '...' , 'ORD_GR393'],
        'callback': each_task,
        'config': {
            'my_endpoint': 'https://your.name/order-handler/'
        },
    },
    'worker': {
        'number': 20,
    }
})

Utilizes all CPU cores on the machine to compute tasks.

import worker_dispatcher

def each_task(id: int, config, task, log):
    result = sum(id * i for i in range(10**9))
    return result

if __name__ == '__main__':
    results = worker_dispatcher.start({
        'task': {
            'list': 10,
            'callback': each_task,
        },
        'worker': {   
            'use_processing': True
        }
    })

INTRODUCTION

This library helps to efficiently consume tasks by using multiple threading or processing and returns all results jointly.

Introduction


INSTALLATION

To install the current release:

$ pip install worker-dispatcher

USAGE

By calling the start() method with the configuration parameters, the package will begin dispatching tasks while managing threading or processing based on the provided settings. Once the tasks are completed, the package will return all the results.

An example configuration setting with all options is as follows:

import worker_dispatcher 

results = worker_dispatcher.start({
    'debug': False,
    'task': {
        'list': [],                     # Support list and integer. Integer represent the number of tasks to be generated.
        'callback': callback_sample,
        'config': {},
        'result_callback': False
    },
    'worker': {
        'number': 8,
        'frequency_mode': {             # Changing from assigning tasks to a fixed number of workers once, to assigning tasks and workers frequently.
            'enabled': False, 
            'interval': 1,              # The second(s) of interval
            'accumulated_workers': 0,   # Accumulate the number of workers for each interval for next dispatch.
            'max_workers': None,        # limit the maximum number of workers to prevent system exhaustion.
        },
        'use_processing': False,        # To break GIL, workers will be based on processing pool.
        'parallel_processing': {        # To break GIL and require a number of workers greater than the number of CPU cores.
            'enabled': False,           # `worker.use_processing` setting will be ignored when enabled. The actual number of workers will be adjusted to a multiple of the CPU core count.
            'use_queue': False,         # Enable a task queue to specify the number of workers without adjustment, though the maximum may be limited by your device.
        },   
    },
    'runtime': None,                    # Dispatcher max runtime in seconds
    'verbose': True
})

Options

OptionTypeDeafultDescription
debugboolFalseDebug mode
task.listmultitypelistThe tasks for dispatching to each worker. *
- List: Each value will be passed as a parameter to your callback function.
- Integer: The number of tasks to be generated.
task.callbackcallable(sample)The callback function called by each worker runs
task.configmultitypelistThe custom variable to be passed to the callback function
task.result_callbackcallableNullThe callback function called when each task processes the result
worker.numberint(auto)The number of workers to fork.
(The default value is the number of local CPU cores)
worker.frequency_mode.enabledboolFalseChanging from assigning tasks to a fixed number of workers once, to assigning tasks and workers frequently.
worker.frequency_mode.intervalfloat1The second(s) of interval.
worker.frequency_mode.accumulated_workersint0Accumulate the number of workers for each interval for next dispatch.
worker.frequency_mode.max_workersintNonelimit the maximum number of workers to prevent system exhaustion.
worker.use_processingbooleanFalseTo break GIL, workers will be based on processing pool.
worker.parallel_processing.enabledboolFalseworker.use_processing setting will be ignored when enabled. The actual number of workers will be adjusted to a multiple of the CPU core count.
worker.parallel_processing.use_queueboolFalseEnable the use of a task queue instead of task dispatch, which allows specifying the number of workers but may be limited by your device.
runtimefloatNoneDispatcher max runtime in seconds.
verboseboolTrueEnables or disables verbose mode for detailed output.
task.callback

The callback function called by each worker runs

callback_function (id: int, config, task, log: dict) -> Any
ArgumentTypeDeafultDescription
idint(auto)The sequence number generated by each task starting from 1
configmultitype{}The custom variable to be passed to the callback function
taskmultitype(custom)Each value from the task.list
logdict{}The log from each task written by this callback function.

The return value can be False to indicate task failure in TPS logs.
Alternatively, it can be a requests.Response, indicating failure if the status code is not 200.

task.result_callback

The callback function called when each task processes the result

result_callback_function (id: int, config, result, log: dict) -> Any
ArgumentTypeDeafultDescription
idint(auto)The sequence number generated by each task starting from 1
configmultitype{}The custom variable to be passed to the callback function
resultmultitype(custom)Each value returned back from task.callback
logdict(auto)Reference: get_logs()

Other Methods

  • get_results()

    Get all results in list type after completing start()

  • get_logs()

    Get all logs in list type after completing start()

    Each log is of type dict, containing the results of every task processed by the worker:

    • task_id
    • started_at
    • ended_at
    • duration
    • result
  • get_result_info()

    Get a dict with the whole spending time and started/ended timestamps after completing start()

  • get_tps()

    Get TPS report in dict type after completing start() or by passing a list data.

    def get_tps(logs: dict=None, debug: bool=False, interval: float=0, reverse_interval: bool = False, display_intervals: bool = False) -> dict:
    

    The log dict matches the format of the get_logs() and refers to it by default. Each task within a log will be validated for success according to the callback_function() result rule.

Scenarios

Stress Test

Perform a stress test scenario with 10 requests per second.

import worker_dispatcher

def each_task(id, config, task, log):
    response = None
    try:
        response = requests.get(config['my_endpoint'], timeout=(5, 10))
    except requests.exceptions.RequestException as e:
        print("An error occurred:", e)
    return response

responses = worker_dispatcher.start({
    'task': {
        'list': 600,
        'callback': each_task,
        'config': {
            'my_endpoint': 'https://your.name/api'
        },
    },
    # Light Load with 10 RPS
    'worker': {
        'number': 10,
        'frequency_mode': {
            'enabled': True, 
            'interval': 1,
        },
    },
})

print(worker_dispatcher.get_logs())
print(worker_dispatcher.get_tps())

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc