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

GitHub license GitHub stars GitHub forks
PyPI version
PyPI - Wheel PyPI - Format PyPI - Status
PyPI - Implementation
PyPI - Python Version
codebeat badge

See the releases page for details about additions/changes to the package!

Installing the module

Enter the following in your command line:

# if something isn't working properly, try rerunning this
# the problem may have been fixed with a newer version

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
Initializing and using the ThreadWithResult class

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.

python3     # MacOS/Linux
python      # Windows
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!!')

To see why checking getattr(thread, 'result', None) might be necessary for a more complicated scenario, see this modification in a testing module from the yt-videos-list package. NOTE that the result attribute was named failed in this commit (the subclass implementation here assigned the result of the threaded function to self.failed instead of to self.result)!

Verified scenario:

Seeing all available methods and attributes for ThreadWithResult class
python3     # MacOS/Linux
python      # Windows
from save_thread_result import ThreadWithResult

# OR

import save_thread_result

import save_thread_result
Motivation for creating this module

I created this module because I needed to store the result of a thread while running tests for the yt-videos-list module and there seemed to be no simple way to get the result from threading.Thread() without importing other modules, creating a Queue, or creating a list and then storing the result in the list, or doing other hacky things.

Sources I looked at before creating the custom class below
Implementation in yt-videos-list
ThreadWithResult logic

This module is really only 6 lines of code:

import threading

class ThreadWithResult(threading.Thread):
    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None):
        def function():
            self.result = target(*args, **kwargs)
        super().__init__(group=group, target=function, name=name, daemon=daemon)

For a more detailed explanation, read through the docstrings in the thread_with_result module. This is also accessible through the python interpreter with

python3     # MacOS/Linux
python      # Windows
import save_thread_result
Usage Statistics

PyPI - Daily Downloads PyPI - Weekly Downloads PyPI - Monthly Downloads
PePY Weekly Downloads PePY Monthly Downloads PePY Total Downloads

If you found this interesting or useful, please consider starring this repo at GitHub so other people can more easily find and use this. Thanks!

Project details

Download files

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

Files for save-thread-result, version 0.0.8
Filename, size File type Python version Upload date Hashes
Filename, size save-thread-result-0.0.8.tar.gz (16.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page