A Python library providing a user-friendly interface for managing both synchronous and asynchronous threads.
Project description
pythread 🧵
A Python library providing a user-friendly interface for managing both synchronous and asynchronous threads.
Description 📚
pythread is a dual-threading Python library crafted to streamline the creation and management of threads in Python applications. It offers two distinct managers:
- SyncThreadManager: Handles classic synchronous threads for executing blocking operations.
- AsyncThreadManager: Manages asynchronous tasks to run coroutines in a non-blocking fashion.
With pythread, starting, stopping, and supervising the life cycle of threads and async tasks becomes effortless, enhancing the readability and resilience of your code.
Features ✨
- Initiate and terminate threads/tasks by name or reference.
- Accommodates additional arguments for threads/tasks.
- Simple management of thread/task lifecycle.
- Suitable for both sync and async implementations.
Installation 📥
To get started with pythread, run:
pip install pythread
Use Cases 🔧
- Conducting background tasks like I/O operations, processing data, or scheduled tasks.
- Handling multiple network connections in parallel.
- Implementing producer-consumer patterns using thread-safe queues.
- Crafting a basic task scheduler for time-based task execution.
Documentation 📄
SyncThreadManager
Facilitates the creation of synchronous threads for running functions with specific delays.
# Initialize the SyncThreadManager
manager = SyncThreadManager()
# Function to run in a separate thread
def print_message(message):
print(f"Thread message: {message}")
# Start a new thread with a specific action and delay
thread = manager.start_thread(name='PrinterThread', func=print_message, delay=1,
message='Hello from SyncThread!'
)
# Stop the thread using its name
manager.stop_thread('PrinterThread')
# Stop the thread using the thread object
manager.stop_thread(thread)
AsyncThreadManager
Enables the execution of async coroutines concurrently.
import asyncio
from pythread import AsyncThreadManager
# Initialize the AsyncThreadManager
manager = AsyncThreadManager()
# Example coroutine function
async def async_print_message(message):
print(f"Async message: {message}")
# Start and run an async task
loop = asyncio.get_event_loop()
task = loop.run_until_complete(manager.start_task(name='AsyncPrinter', coro_func=async_print_message, message='Hello from AsyncThread!'))
# Stop the task using its name
loop.run_until_complete(manager.stop_task('AsyncPrinter'))
# Stop the task using the task object
loop.run_until_complete(manager.stop_task(task))
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
File details
Details for the file pythread-1.5.0.tar.gz
.
File metadata
- Download URL: pythread-1.5.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13cb5eb67cd216ab87b4195a3e0349061e41be04b353c93f37d7f3c36d27b911 |
|
MD5 | 22c6bdda8caa0a0c374d71b953f29de1 |
|
BLAKE2b-256 | 897379aabc468db4598ce27e982a92b05d418c435084d5d2213295f947833572 |
File details
Details for the file pythread-1.5.0-py3-none-any.whl
.
File metadata
- Download URL: pythread-1.5.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd42b5570e4a4ddc0d7e434a612c4dd2307dc4f62653f83bc91b0915bf0f1eea |
|
MD5 | 18f68ee839958f2cdcd3477067013688 |
|
BLAKE2b-256 | 152a703e2b8f0b781beedf0ca8f88309e6554b36a36e487a49dcb711b44ffb9a |