Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zechcodes/oodle
Oodle is a Python package that makes it easier to manage threads.
https://github.com/zechcodes/oodle
free-threaded free-threading python python3 thread-groups threading
Last synced: 1 day ago
JSON representation
Oodle is a Python package that makes it easier to manage threads.
- Host: GitHub
- URL: https://github.com/zechcodes/oodle
- Owner: ZechCodes
- License: mit
- Created: 2024-11-28T17:14:14.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-12-09T02:03:44.000Z (about 1 month ago)
- Last Synced: 2024-12-09T03:19:02.258Z (about 1 month ago)
- Topics: free-threaded, free-threading, python, python3, thread-groups, threading
- Language: Python
- Homepage:
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Oodle
Oodle is a package that makes it easier to manage threads.
## Installation
```bash
pip install oodle
```## Usage
```python
from oodle import spawndef foo(message):
print(message)
spawn[foo]("Hello World!").wait()
```That spawns a thread, runs the function `foo` with the argument `"Hello World!"`, and waits for it to finish.
Spawned threads return an `oodle.threads.Thread` which provides a `wait` method that blocks until the thread finishes and an `is_alive` property that returns `True` if the thread is still running.
### Thread Groups
```python
from oodle import ThreadGroupdef foo(message):
print(message)with ThreadGroup() as group:
group.spawn[foo]("Hello World!")
group.spawn[foo]("Goodbye World!")
```That spawns two threads, runs the function `foo` with the argument `"Hello World!"` in one thread and `"Goodbye World!"` in the other, and waits for both to finish.
If any thread in a thread group raises an exception, all other threads are stopped and the exception is raised in the calling thread.
### Channels
```python
from oodle import Channel, ThreadGroupdef foo(message, channel):
channel.put(message)with Channel() as channel:
with ThreadGroup() as group:
group.spawn[foo]("Hello World!", channel)
group.spawn[foo]("Goodbye World!", channel)message_a, message_b = channel
print(message_a, message_b)
```Channels also provide a `get` method and an `is_empty` property.
Additionally, the channel type provides a way to select the first message that arrives and stop all threads.
```python
from oodle import Channel
from oodle.utilities import sleepdef foo(channel):
channel.put("Hello World!")
def bar(channel):
sleep(1)
channel.put("Goodbye World!")
result = Channel.get_first(foo, bar)
print(result) # "Hello World!"
```Internally this uses a thread group to spawn the functions and a channel to communicate between them. So if any of the functions raises an exception, all other threads are stopped and the exception is raised in the calling thread. If no exception is raised, all threads will be stopped after the first message arrives.
### Shields
Threads can use shields to protect against interruption during critical sections.
```python
from oodle import Shield, spawn, sleepdef foo():
with Shield():
sleep(1)thread = spawn[foo]()
thread.stop(0.1) # Raises TimeoutError
```### Blocking & Patching
To enable thread interruption it is necessary to not use anything that can block the thread indefinitely. A great example is `time.sleep`. To avoid this use `oodle.sleep` instead. It is possible to patch `time.sleep` with `oodle.sleep` by importing `oodle.patches.patch_time` before any other modules.
```python
import oodle.patches
oodle.patches.patch_time()from time import sleep
```