Skip to main content

Library for easy multithreading.

Project description

ReThread

Minimalistic Python Library for Easy Multithreading

Installation

Linux/Mac

python3 -m pip install -U rethread

Windows

py -3 -m pip install -U rethread

Example

import rethread
import time

@rethread.auto
def my_long_function():
    time.sleep(10)

    return 'a'

def some_other_function():
    for i in range(3):
        time.sleep(1)
        print(i)

def another_function(t: rethread.RunningThread):
    time.sleep(10)

    if t.done:
        print('thread is finished!')


with my_long_function() as t:
    some_other_function()
    another_function(t)

print(thread.value)

Usage

To create a thread, you can use the rethread.thread function, like so:

import rethread

def long_function():
    ...

thread: rethread.RunningThread = rethread.thread(long_function)

If you would like to pass in parameters, simply pass them in to the *args and **kwargs of the rethread.thread call. For example:

import rethread

def long_function(a: str, b: str, some_kwarg: str = 'c'):
    ...

thread: rethread.RunningThread = rethread.thread(long_function, 'a', 'b', some_kwarg = 'c')

If you plan on always running a function in a thread, you can use rethread.auto to automatically thread a function:

@rethread.auto
def long_function():
    ...

thread: rethread.RunningThread = long_function() # no need for a call to rethread.thread

To get the return value of the threaded function, access the RunningThread.value attribute.

An error will be raised if the thread is still running, so make sure to call wait() on the thread.

@rethread.auto
def long_function() -> str:
    ...

    return 'hi'

t = long_function()
t.wait() # wait for the thread to finish
print(t.value) # hi

Alternatively, you can use the context manager syntax to automatically wait for the thread to finish:

@rethread.auto
def long_function() -> str:
    ...

    return 'hi'

t = long_function()
with t:
    do_something()
    # once everything in this context is finished, rethread automatically waits for the thread to finish

print(t.value) # hi

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

rethread-1.0.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

rethread-1.0.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file rethread-1.0.1.tar.gz.

File metadata

  • Download URL: rethread-1.0.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for rethread-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2b5f073bdd58313a68fe866c0d77b59795a3c609685d19b0c5d70f278fa0f8c5
MD5 d64ce8f93a4f8980b3fde46c2c5e3047
BLAKE2b-256 55f9226ab4bdc57b3d015f08cde3d7a329d0e9059e25a5a5c06fdfc6ce3c6c3d

See more details on using hashes here.

File details

Details for the file rethread-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: rethread-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for rethread-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e4b6b9515796ce9b534e0075753176e60ab5bab4c3905100630cf92898affb6
MD5 3a6cae87697924c1004afc6a82a33e61
BLAKE2b-256 858e40d040dbee82708c8d99e54580edd14d46baabc2f81d37c0c3dafa49174d

See more details on using hashes here.

Supported by

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