oodle


Nameoodle JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummaryOodle is a package that makes it easier to manage threads.
upload_time2024-12-04 18:47:19
maintainerNone
docs_urlNone
authorZech Zimmerman
requires_python<4.0,>=3.13
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Oodle

Oodle is a package that makes it easier to manage threads.

## Installation

```bash
pip install oodle
```

## Usage

```python
from oodle import spawn


def 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 ThreadGroup


def 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, ThreadGroup

def 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 sleep

def 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, sleep


def 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
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "oodle",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.13",
    "maintainer_email": null,
    "keywords": null,
    "author": "Zech Zimmerman",
    "author_email": "hi@zech.codes",
    "download_url": "https://files.pythonhosted.org/packages/05/88/c4cd015a3fcb4be62ef2c94008ce88bbdc19cdd8207d9bef39e37b2dc65c/oodle-0.1.4.tar.gz",
    "platform": null,
    "description": "# Oodle\n\nOodle is a package that makes it easier to manage threads.\n\n## Installation\n\n```bash\npip install oodle\n```\n\n## Usage\n\n```python\nfrom oodle import spawn\n\n\ndef foo(message):\n    print(message)\n\n    \nspawn[foo](\"Hello World!\").wait()\n```\n\nThat spawns a thread, runs the function `foo` with the argument `\"Hello World!\"`, and waits for it to finish.\n\nSpawned 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.\n\n### Thread Groups\n\n```python\nfrom oodle import ThreadGroup\n\n\ndef foo(message):\n    print(message)\n\n\nwith ThreadGroup() as group:\n    group.spawn[foo](\"Hello World!\")\n    group.spawn[foo](\"Goodbye World!\")\n```\n\nThat 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.\n\nIf any thread in a thread group raises an exception, all other threads are stopped and the exception is raised in the calling thread.\n\n### Channels\n\n```python\nfrom oodle import Channel, ThreadGroup\n\ndef foo(message, channel):\n    channel.put(message)\n\nwith Channel() as channel:\n    with ThreadGroup() as group:\n        group.spawn[foo](\"Hello World!\", channel)\n        group.spawn[foo](\"Goodbye World!\", channel)\n\n    message_a, message_b = channel\n    print(message_a, message_b)\n```\n\nChannels also provide a `get` method and an `is_empty` property.\n\nAdditionally, the channel type provides a way to select the first message that arrives and stop all threads.\n\n```python\nfrom oodle import Channel\nfrom oodle.utilities import sleep\n\ndef foo(channel):\n    channel.put(\"Hello World!\")\n    \ndef bar(channel):\n    sleep(1)\n    channel.put(\"Goodbye World!\")\n    \nresult = Channel.get_first(foo, bar)\nprint(result)  # \"Hello World!\"\n```\n\nInternally 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.\n\n### Shields\n\nThreads can use shields to protect against interruption during critical sections.\n\n```python\nfrom oodle import Shield, spawn, sleep\n\n\ndef foo():\n    with Shield():\n        sleep(1)\n\n\nthread = spawn[foo]()\nthread.stop(0.1)  # Raises TimeoutError\n```\n\n### Blocking & Patching\n\nTo 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.\n\n```python\nimport oodle.patches\noodle.patches.patch_time()\n\nfrom time import sleep\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Oodle is a package that makes it easier to manage threads.",
    "version": "0.1.4",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "646c8ca7ce4c7d19dbe8ab7c5e6f8b3af7fca19c5e2075c1d1306948c4e607dd",
                "md5": "fd6d32ab1c62547c5055d01b4705e4d4",
                "sha256": "1fdf66b21f1be028e590d151463b18d9dd1cf7baf417caef75de67ce14e8b851"
            },
            "downloads": -1,
            "filename": "oodle-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fd6d32ab1c62547c5055d01b4705e4d4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.13",
            "size": 7990,
            "upload_time": "2024-12-04T18:47:17",
            "upload_time_iso_8601": "2024-12-04T18:47:17.613439Z",
            "url": "https://files.pythonhosted.org/packages/64/6c/8ca7ce4c7d19dbe8ab7c5e6f8b3af7fca19c5e2075c1d1306948c4e607dd/oodle-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0588c4cd015a3fcb4be62ef2c94008ce88bbdc19cdd8207d9bef39e37b2dc65c",
                "md5": "4e394581e42dc39b1eccd0c962e8f3b6",
                "sha256": "1c897a0950bc5e9c7a08b5486a7b4421df0804a2bfed458768cf5780ef82cb67"
            },
            "downloads": -1,
            "filename": "oodle-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "4e394581e42dc39b1eccd0c962e8f3b6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.13",
            "size": 6339,
            "upload_time": "2024-12-04T18:47:19",
            "upload_time_iso_8601": "2024-12-04T18:47:19.178776Z",
            "url": "https://files.pythonhosted.org/packages/05/88/c4cd015a3fcb4be62ef2c94008ce88bbdc19cdd8207d9bef39e37b2dc65c/oodle-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-04 18:47:19",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "oodle"
}
        
Elapsed time: 1.39248s