Skip to main content

python simple multi threading task queue

Project description

Background Task Queue

Overview

Installation

pip install bg-task-queue

Example

import time
from src.bg_task_queue import BackgroundTaskQueue

tq = BackgroundTaskQueue(verbose=True, max_workers=2)


# add function using decorator
@tq.function
def task_a(i):
    time.sleep(1)
    print(f'Task_a({i}) Finished')


# or add manually
def task_b(a=0):
    time.sleep(3)
    print(f'Task_b({a}) Finished')


tq.function(task_b)

tq.add_task("task_a", 100)
tq.add_task("task_b", a=200)
tq.add_task("task_a", 300)
tq.add_task("task_a", 400)

while True:
    summary = {"pending": 0, "running": 0, "stopped": 0, "errored": 0, "success": 0}
    tasks = tq.get_all_tasks()
    for t in tq.get_all_tasks():
        print(t)
        summary[t.status] += 1
    print(summary)
...
[*] Worker-1 : End 71f323e7-5b25-4870-b22d-667dda0c5749
[*] Worker-1 : takes job 3472f262-8a7d-446c-a601-fb5a02284f22
[*] Worker-1 : Start 3472f262-8a7d-446c-a601-fb5a02284f22
<Task id=71f323e7-5b25-4870-b22d-667dda0c5749, status=success function=task_a, args=[100], kwargs={}>
<Task id=8220b58b-3087-43d2-a813-cd2c0c8458e0, status=running function=task_b, args=[], kwargs={"a": 200}>
<Task id=3472f262-8a7d-446c-a601-fb5a02284f22, status=running function=task_a, args=[300], kwargs={}>
<Task id=6c7c27fc-ccc5-419b-8b92-61b5738dfa51, status=pending function=task_a, args=[400], kwargs={}>
{'pending': 1, 'running': 2, 'stopped': 0, 'errored': 0, 'success': 1}
...

Classes

Status (Enum)

  • PENDING: The task is pending execution.
  • RUNNING: The task is currently running.
  • SUCCESS: The task successfully completed.
  • ERROR: The task ran into an error during execution.
  • STOPPED: The task has been stopped.

Task

  • id: Unique identifier for the task.
  • func_name: The name of the function to run.
  • args: Positional arguments for the function.
  • kwargs: Keyword arguments for the function.
  • status: The current status of the task. Default is PENDING.

BackgroundTaskQueue

  • db_path: Database path for SQLite3. Default is :memory:.
  • max_workers: Maximum number of worker threads. Default is 2.
  • verbose: Enables verbose logging. Default is False.
  • falling_rate: The sleep time between each worker loop. Default is 0.1.

License

This project is licensed under the MIT License. See LICENSE.md for more 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

bg_task_queue-0.4.14.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

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

bg_task_queue-0.4.14-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file bg_task_queue-0.4.14.tar.gz.

File metadata

  • Download URL: bg_task_queue-0.4.14.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for bg_task_queue-0.4.14.tar.gz
Algorithm Hash digest
SHA256 766a7bd1e01769527ccd4d84122b28e42e99965fddd3ee279969c9970adc12da
MD5 d423804e6b792f3139c89183d7f68bb2
BLAKE2b-256 6e4bd6ed84842464922a73bb9f40cca7b005e171e959f3ce5294c6ec81342a00

See more details on using hashes here.

File details

Details for the file bg_task_queue-0.4.14-py3-none-any.whl.

File metadata

  • Download URL: bg_task_queue-0.4.14-py3-none-any.whl
  • Upload date:
  • Size: 2.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for bg_task_queue-0.4.14-py3-none-any.whl
Algorithm Hash digest
SHA256 543ec1090b4f4da850bde070e90bd26d1c3704736ee7c0c6bd1a8f92421aec44
MD5 efa5a1d81458692423ec93cee4f2483f
BLAKE2b-256 44dcf3179f801a2600ed7c416dae2bfa8ed02f5fe774e85add6d3b10c78c1dbf

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