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.
Metadata
Release files for threadyq 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| threadyq-1.0.0.tar.gz | 4.8 kB | Without provenance | Details for threadyq-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| threadyq-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for threadyq-1.0.0-py3-none-any.whl |
Total release size: 9.5 kB
Release files / threadyq-1.0.0.tar.gz
| Download URL | threadyq-1.0.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f97e6d327b9829069debf67eebf85acfa3276f8c5b1cb490a0269ac46813106
|
|
BLAKE2b-256 checksum How to use checksums |
981ac3f49d1fb33334718ec5b50f818ba77efed5edb1469acf0064632078df72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.4
|
Release files / threadyq-1.0.0-py3-none-any.whl
| Download URL | threadyq-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b78b32ad98e09bf31ecd6c28010749ca5d0fbbf5fa9dbbfd61fe59e6ef8a41c9
|
|
BLAKE2b-256 checksum How to use checksums |
469be98ed1f7399f86e816220c7512fc6a898de3dfd601216687c71516db2df8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.4
|
Security for threadyq 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.