Python3 thread based async queue manager
Project description
queue_processor Python3 Queue
Homepage: https://www.learningtopi.com/python-modules-applications/queue_processor/
This package is a simple threading based queue manager. A queue can be created with a specific maximum length as well as default command and final callback functions. When an item is added to the queue, the queue_processor will create a thread to execute the command function that was either provided when the item was added, or the default function for the queue. Arguments will be passed to the function and the return value will be captured. If a funal callback function is provided, the return value as well as the status will be returned along with a copy of the arguments.
The purpose of this class is to provide a modular and reusable queue that can be used in many different projects for any async tasks that need to be performed in sequence. The queue_processor operates as a FIFO queue and can provide a delay between tasks (i.e. for an IR transmitter where a pause is needed).
Use Example
from queue_processor import QueueManager, STATUS_OK, STATUS_TIMEOUT, STATUS_EXPIRED, STATUS_QUEUE_FULL, STATUS_EXCEPTION
def task(arg1, arg2):
print(f'executing task with {arg1}, {arg2}')
return arg1
def finished_callback(return_value, status, *args, **kwargs):
print(f'Completed task return value: {return_value}, status {status}, args: {args}, kwargs: {kwargs}')
queue = QueueManager(name='test1', command_func=task, callback_func=finished_callback)
queue.add(kwargs={'arg1': 1, 'arg2': 2})
Command Function
The command function can be any callable function (including a function that is part of a class). The function will be passed all the positional and keyword arguments that are supplied when adding the instance to the queue.
Callback Function
The callback function can be any callable function (including a function that is part of a class). This OPTIONAL function is called after the command function either completes or times out. The callback will provide the return value of the command function (if any) as well as a status that will be one of the following:
- OK: Command function completed (may or may not be successful in your view, but the function completed and did not raise any errors)
- TIMEOUT: If the command function did not complete within the timeout period.
- EXPIRED: The item was NOT executed as it sat in the queue longer than the max time permitted.
- QUEUE_FULL: This is returned if a callback is provided when an item is attempted to be added but the queue is full. This item is NOT executed.
- EXCEPTION: An exception was raised during the execution of the callback. The exception is returned as the "return_value"
Callback function parameters
The callback function must accept the "return_value" and "status" as positional arguments in that order. It is strongly recommended to include *args and **kwargs to catch all positional and keyword arguments that are sent after "return_value" and "status". The queue processor will send ALL arguments to the callback function that were also sent to the command function.
NOTE: You may send objects as parameters that will be updated by the command function! You may also have the command function be a member of a class instance. This allows you to act on data within an instance of a class so passing objects may not be required.
!!IMPORTANT!! The queue processor uses Python threads. In your command and callback function be sure to use thread locks appropriately if modifying data that may be accessed by a different thread. Also be sure not to create deadlocks!
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
Built Distribution
File details
Details for the file queue_processor-1.1.2.tar.gz
.
File metadata
- Download URL: queue_processor-1.1.2.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff391440d00523fd2c430d8c8219e3665ddaac81d3b6dc5649019565f8804329 |
|
MD5 | 1ab881f9d19aa80a91f4f6850e026aa3 |
|
BLAKE2b-256 | d6332adfd9f0c2b33f09d5e20cbf85f9e2091b8dcdacb14e57bf7f5df19bc0e1 |
File details
Details for the file queue_processor-1.1.2-py3-none-any.whl
.
File metadata
- Download URL: queue_processor-1.1.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10c800fc71be44bc7da8fea3ce29e1abbad44f4f89e23bf80d686383ed7fc608 |
|
MD5 | e134c291732e1a466ee73a712d9f4b7d |
|
BLAKE2b-256 | e8c18cf745a1e35b17f19dba9cb76bf8c442ce743ee3fae12aa71ad925dd7e31 |