Giter Site home page Giter Site logo

dedinc / pythread Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 2.0 5 KB

A Python library providing a user-friendly interface for managing both synchronous and asynchronous threads.

Home Page: https://pypi.org/project/pythread

License: MIT License

Python 100.00%
threading thread pythread manager thread-manager thread-management async-manager asyncio task-manager

pythread's Introduction

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))

pythread's People

Contributors

dedinc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.