Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lamaani/zthreading.py
A collection of wrapper classes for event broadcast and task management for python (Python Threads or Asyncio)
https://github.com/lamaani/zthreading.py
Last synced: 13 days ago
JSON representation
A collection of wrapper classes for event broadcast and task management for python (Python Threads or Asyncio)
- Host: GitHub
- URL: https://github.com/lamaani/zthreading.py
- Owner: LamaAni
- Created: 2020-06-23T13:45:24.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-21T03:08:06.000Z (over 2 years ago)
- Last Synced: 2024-12-30T16:27:53.534Z (about 1 month ago)
- Language: Python
- Size: 69.3 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# zthreading.py
A collection of wrapper classes for event broadcast and task management for python (Python Threads or Asyncio)
### If you like it, star it, so other people may use it!
# TL;DR
## Events
```python
from zthreading.events import EventHandlerhandler = EventHandler()
def handle_test_event(msg: str):
print("The event messge: " + msg)handler.on("test", handle_test_event)
handler.emit("test", "the message")
```## Tasks
```python
from zthreading.events import EventHandler
from zthreading.tasks import Taskhandler = EventHandler()
def handle_test_event(msg: str):
print("The event messge: " + msg)def run_in_a_different_thread(msg):
handler.emit("test", msg)handler.on("test", handle_test_event)
Task(run_in_a_different_thread).start("A message from a thread").join()
```## Decorators
```python
from zthreading.decorators import collect_consecutive_calls_async, as_task, catch_signal
from singal import Signals, raise_signal
from zthreading.tasks import Taskfor i in range(1, 20):
consecutive_calls_action()@as_task
def my_method_as_task(a:int,b:str):
print(b+str(a))my_task:Task = my_method_as_task("a",1)
my_task.join()@catch_signal(Signals.SIGTERM)
def my_signal_ignore_method():
# will be ignored.
raise_signal(Signals.SIGTERM)@collect_consecutive_calls_async()
def consecutive_calls_action(): # Like save this to file.. for example.
# should be printed twice, once for the first call, and another for the last call.
print("consecutive called action")
```See decorator help for more
# Environment variables
1. TASKS_DEFAULT_TO_ASYNC_LOOP - If set to "true", will default all tasks to use asyncio.
# Advanced Methods and capabilities
Note: The task object is an EventHandler and has all the capabilities of one.
## Task wait functions (staticmethod)
(Examples in code)
1. Task.wait_for_all (tasks.... )
1. Task.wait_for_some (tasks... )
1. Task.wait_for_one (tasks...)
1. Task.wait_for_events(tasks, event names....)## Piping events
Transferring events from one handler to another. If a weak reference is used
then then the second handler can be deleted by garbage collection.```python
from zthreading.events import EventHandlerhandler_a = EventHandler()
handler_b = EventHandler()# Transfer all events to handler b, as
# long as handler b is object is in memory. Will
# not keep handler_b in memory.
handler_a.pipe(handler_b, use_weak_reference=True)def handle_test_event(msg: str):
print("The event messge: " + msg)handler_b.on("test", handle_test_event)
handler_a.emit("test", "The piped message")
```## Streaming events and using tasks to do it.
Events can be streamed (yield generator),
```python
from random import random
from time import sleep
from zthreading.tasks import Task# A task is an EventHandler, and has the
# on method as well.
task: Task = Nonedef invoke_timed_events():
sleep(1)
for i in range(1, 10):
sleep(random() / 10)
task.emit("test", f"loop index {i}")
task.stop_all_streams()task = Task(invoke_timed_events).start()
for ev in task.stream("test"):
print(f"{ev.name}, {ev.args[0]}")
```# Install
```shell
pip install zthreading
```## From the git repo directly
To install from master branch,
```shell
pip install git+https://github.com/LamaAni/zthreading.py.git@master
```To install from a release (tag)
```shell
pip install git+https://github.com/LamaAni/zthreading.py.git@[tag]
```# Contribution
Feel free to ping me in issues or directly on LinkedIn to contribute.
# Licence
Copyright ©
`Zav Shotan` and other [contributors](https://github.com/LamaAni/postgres-xl-helm/graphs/contributors).
It is free software, released under the MIT licence, and may be redistributed under the terms specified in `LICENSE`.