Skip to main content

Simple subclass wrapper around `threading.Thread` to get the return value from a thread in python. Exact same interface as `threading.Thread`! 🌟 Star this repo if you found it useful! 🌟

Project description

Python API


Installing the save_thread_result module:

pip3 install -U save-thread-result     # MacOS/Linux
pip  install -U save-thread-result     # Windows

# if that doesn't work:
python3 -m pip install -U save-thread-result     # MacOS/Linux
python  -m pip install -U save-thread-result     # Windows

Using the ThreadWithResult class from the save_thread_result module:

from save_thread_result import ThreadWithResult

# As of Release 0.0.3, you can also specify values for
# `group`, `name`, and `daemon` if you want to set those
# values manually.

thread = ThreadWithResult(
    target = my_function,
    args   = (my_function_arg1, my_function_arg2, ...)
    kwargs = {my_function_kwarg1: kwarg1_value, my_function_kwarg2: kwarg2_value, ...}

if getattr(thread, 'result', None):
    # thread.result attribute not set - something caused
    # the thread to terminate BEFORE the thread finished
    # executing the function passed in through the
    # `target` argument
    print('ERROR! Something went wrong while executing this thread, and the function you passed in did NOT complete!!')

Reading the documentation for more information:

from save_thread_result import ThreadWithResult

Short explanation

This module uses a threading.Thread subclass ThreadWithResult that saves the result of a thread (from threading built-in module in the Python Standard library) as its result attribute - i.e. after the thread finishes running, call thread.result to get the return value from the function that ran on that thread.


Dummy example:

from save_thread_result import ThreadWithResult

import time, random, threading

def function_to_thread(n):
    count = 0
    while count < 3:
            print(f'Still running {threading.current_thread().name}...')
            count +=1
    result = random.random()
    print(f'Return value of {threading.current_thread().name} should be: {result}')
    return result

def main():
    thread1 = ThreadWithResult(target=function_to_thread, args=(1,))
    thread2 = ThreadWithResult(target=function_to_thread, args=(2,))
    print(f'The `result` attribute of {} is: {thread1.result}')
    print(f'The `result` attribute of {} is: {thread2.result}')

More information
Sources I looked at before creating the custom class

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

save-thread-result-0.1.1.post1.tar.gz (16.7 kB view hashes)

Uploaded source

Built Distribution

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