Skip to main content

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

Project description

Oodle

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

Installation

pip install oodle

Usage

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

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

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.

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.

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.

import oodle.patches
oodle.patches.patch_time()

from time import sleep

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oodle-0.1.4.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oodle-0.1.4-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file oodle-0.1.4.tar.gz.

File metadata

  • Download URL: oodle-0.1.4.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Linux/6.5.0-1025-azure

File hashes

Hashes for oodle-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1c897a0950bc5e9c7a08b5486a7b4421df0804a2bfed458768cf5780ef82cb67
MD5 4e394581e42dc39b1eccd0c962e8f3b6
BLAKE2b-256 0588c4cd015a3fcb4be62ef2c94008ce88bbdc19cdd8207d9bef39e37b2dc65c

See more details on using hashes here.

File details

Details for the file oodle-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: oodle-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Linux/6.5.0-1025-azure

File hashes

Hashes for oodle-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1fdf66b21f1be028e590d151463b18d9dd1cf7baf417caef75de67ce14e8b851
MD5 fd6d32ab1c62547c5055d01b4705e4d4
BLAKE2b-256 646c8ca7ce4c7d19dbe8ab7c5e6f8b3af7fca19c5e2075c1d1306948c4e607dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page