ThreadyQ is a Python library designed to simplify and extend the capabilities of threading and queue management. This package provides an abstraction layer over Python's built-in threading and queue modules, enabling developers to create, manage, and synchronize tasks efficiently in multi-threaded applications.
Project description
ThreadyQ
ThreadyQ is a Python library designed to simplify and extend the capabilities of threading and queue management. This package provides an abstraction layer over Python's built-in threading and queue modules, enabling developers to create, manage, and synchronize tasks efficiently in multi-threaded applications.
ThreadyQ is a simple Python utility that provides a thread-safe task queue managed by a background worker thread. It enables asynchronous execution of tasks with minimal setup.
Features
-
Thread-safe task queue
-
Asynchronous task execution
-
Graceful shutdown and completion handling
-
Colored console output for responses
Requirements
This utility works with:
- Python 3.6+
Installation
Copy the ThreadyQ class code into your project or save it as threadyq.py. Import it wherever needed.
Usage
Importing ThreadyQ
from threadyq import ThreadyQ
Creating a Task Queue
# Create an instance of ThreadyQ
tq = ThreadyQ()
Adding Tasks
You can add tasks to the queue using the put_the_task method. Each task must be a callable function.
# Define a sample task
def sample_task(arg1, arg2):
print(f"Task executed with arguments: {arg1}, {arg2}")
# Add the task to the queue
tq.put_the_task(sample_task, "Hello", "World")
Monitoring Tasks
You can get the number of pending tasks in the queue:
print(f"Pending tasks: {tq.get_task_count()}")
Waiting for All Tasks to Complete
To block the main thread until all tasks in the queue are completed:
tq.join()
Example
Here is a complete example:
from threadyq import ThreadyQ
def sample_task(arg1, arg2):
print(f"Task executed with arguments: {arg1}, {arg2}")
# Initialize ThreadyQ
tq = ThreadyQ()
# Add tasks to the queue
tq.put_the_task(sample_task, "Hello", "World")
tq.put_the_task(sample_task, "Foo", "Bar")
# Wait for all tasks to complete
tq.join()
Output
Task sample_task added. Current task count: 1
Task sample_task added. Current task count: 2
Task executed with arguments: Hello, World
Task executed with arguments: Foo, Bar
All tasks completed.
Customization
Print Colored Messages
You can customize the output of print_response to provide color-coded messages for success or error logs.
Worker Daemon Mode
By default, the worker thread runs as a daemon. You can change this behavior by passing daemon=False while initializing ThreadyQ:
tq = ThreadyQ(daemon=False)
Limitations
-
The worker thread doesn’t have a direct shutdown mechanism in this version. To improve it, you can implement a stop signal as shown in enhanced versions.
-
It’s best suited for lightweight task management. For large-scale or complex systems, consider frameworks like Celery.
Contributing
Feel free to contribute to ThreadyQ by submitting pull requests or reporting issues.
License
This utility is open-source and free to use. Licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file threadyq-1.0.0.tar.gz.
File metadata
- Download URL: threadyq-1.0.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f97e6d327b9829069debf67eebf85acfa3276f8c5b1cb490a0269ac46813106
|
|
| MD5 |
0830556215adccaf21e654d840cf38e6
|
|
| BLAKE2b-256 |
981ac3f49d1fb33334718ec5b50f818ba77efed5edb1469acf0064632078df72
|
File details
Details for the file threadyq-1.0.0-py3-none-any.whl.
File metadata
- Download URL: threadyq-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b78b32ad98e09bf31ecd6c28010749ca5d0fbbf5fa9dbbfd61fe59e6ef8a41c9
|
|
| MD5 |
38f3f2242993ee1864ec075a76a70130
|
|
| BLAKE2b-256 |
469be98ed1f7399f86e816220c7512fc6a898de3dfd601216687c71516db2df8
|