Skip to main content

Python API

Quickstart

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, ...}
)

thread.start()
thread.join()
if getattr(thread, 'result', None):
    print(thread.result)
else:
    # 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
help(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.

Examples

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
            time.sleep(3)
    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,))
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    print(f'The `result` attribute of {thread1.name} is: {thread1.result}')
    print(f'The `result` attribute of {thread2.name} is: {thread2.result}')

main()
More information
Sources I looked at before creating the custom class

Release files for save-thread-result 0.1.1.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for save-thread-result 0.1.1.post1
File Size Uploaded
save-thread-result-0.1.1.post1.tar.gz 16.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for save-thread-result 0.1.1.post1
File Interpreter ABI Platform
save_thread_result-0.1.1.post1-py3-none-any.whl Python 3 none any Details

Total release size:27.0 kB

Release files / save-thread-result-0.1.1.post1.tar.gz

Download URL save-thread-result-0.1.1.post1.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
46a214c1e204a8c4f92d293d0c121bb7bd38db98d79dc9b9384edce9dfbf0cbd
BLAKE2b-256 checksum
How to use checksums
6d45c05c254fb5e074f08a66882a9b0524509bf61a9c4d2b32409bf5ba13b84d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / save_thread_result-0.1.1.post1-py3-none-any.whl

Download URL save_thread_result-0.1.1.post1-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f78b4eac2e49b0978c86d2a0d5465dda5ec2f47c94c202b323e57d74eca3a34
BLAKE2b-256 checksum
How to use checksums
af6dfbf21b350439dc05bb7c8ed8fccd35f9be7186c1621cf8a08a6f1e2626bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.1.1.post1 This release

2 release files

0.1.1

2 release files

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page