# 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`](https://docs.python.org/3/library/threading.html#threading.Thread) subclass `ThreadWithResult` that saves the result of a thread (from [`threading`](https://docs.python.org/3/library/threading.html) built-in module in the [Python Standard library](https://docs.python.org/3/library/index.html)) 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()
```
<details>
<summary><b>More information</b></summary>
<details>
<summary><b>Sources I looked at before creating the custom class</b></summary>
- [Return value from thread](https://stackoverflow.com/questions/1886090/return-value-from-thread)
- [Threading in python: retrieve return value when using target= [duplicate]](https://stackoverflow.com/questions/2577233/threading-in-python-retrieve-return-value-when-using-target)
- [How to get the return value from a thread in python?](https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python)
- [Using Python Threading and Returning Multiple Results (Tutorial)](https://www.shanelynn.ie/using-python-threading-for-multiple-results-queue/)
- [How to get the return value from a thread using python](https://www.edureka.co/community/31966/how-to-get-the-return-value-from-a-thread-using-python)
- [How to manage python threads results?](https://stackoverflow.com/questions/3239617/how-to-manage-python-threads-results#3239815)
- [How to obtain the results from a pool of threads in python?](https://stackoverflow.com/questions/26104512/how-to-obtain-the-results-from-a-pool-of-threads-in-python)
- [Google search](https://www.google.com/search?hl=en&q=python%20save%20thread%20result)
</details>
</details>
Raw data
{
"_id": null,
"home_page": "https://github.com/slow-but-steady/save-thread-result/tree/main/python",
"name": "save-thread-result",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3, <4",
"maintainer_email": "",
"keywords": "threading thread multi-threading logging logger archiving tracing tracer debugging debugger automation csv txt markdown md YouTube videos URL scraping Selenium macos windows linux",
"author": "slow-but-steady",
"author_email": "slowbutsteady1234@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/6d/45/c05c254fb5e074f08a66882a9b0524509bf61a9c4d2b32409bf5ba13b84d/save-thread-result-0.1.1.post1.tar.gz",
"platform": null,
"description": "# Python API\n\n## Quickstart\n\nInstalling the `save_thread_result` module:\n```\npip3 install -U save-thread-result # MacOS/Linux\npip install -U save-thread-result # Windows\n\n# if that doesn't work:\npython3 -m pip install -U save-thread-result # MacOS/Linux\npython -m pip install -U save-thread-result # Windows\n```\n\nUsing the `ThreadWithResult` class from the `save_thread_result` module:\n```\nfrom save_thread_result import ThreadWithResult\n\n\n# As of Release 0.0.3, you can also specify values for\n# `group`, `name`, and `daemon` if you want to set those\n# values manually.\n\nthread = ThreadWithResult(\n target = my_function,\n args = (my_function_arg1, my_function_arg2, ...)\n kwargs = {my_function_kwarg1: kwarg1_value, my_function_kwarg2: kwarg2_value, ...}\n)\n\nthread.start()\nthread.join()\nif getattr(thread, 'result', None):\n print(thread.result)\nelse:\n # thread.result attribute not set - something caused\n # the thread to terminate BEFORE the thread finished\n # executing the function passed in through the\n # `target` argument\n print('ERROR! Something went wrong while executing this thread, and the function you passed in did NOT complete!!')\n```\n\nReading the documentation for more information:\n```\nfrom save_thread_result import ThreadWithResult\nhelp(ThreadWithResult)\n```\n\n## Short explanation\n\nThis module uses a [`threading.Thread`](https://docs.python.org/3/library/threading.html#threading.Thread) subclass `ThreadWithResult` that saves the result of a thread (from [`threading`](https://docs.python.org/3/library/threading.html) built-in module in the [Python Standard library](https://docs.python.org/3/library/index.html)) 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.\n\n## Examples\n\nDummy example:\n```\nfrom save_thread_result import ThreadWithResult\n\nimport time, random, threading\n\n\ndef function_to_thread(n):\n count = 0\n while count < 3:\n print(f'Still running {threading.current_thread().name}...')\n count +=1\n time.sleep(3)\n result = random.random()\n print(f'Return value of {threading.current_thread().name} should be: {result}')\n return result\n\n\ndef main():\n thread1 = ThreadWithResult(target=function_to_thread, args=(1,))\n thread2 = ThreadWithResult(target=function_to_thread, args=(2,))\n thread1.start()\n thread2.start()\n thread1.join()\n thread2.join()\n print(f'The `result` attribute of {thread1.name} is: {thread1.result}')\n print(f'The `result` attribute of {thread2.name} is: {thread2.result}')\n\nmain()\n```\n\n<details>\n <summary><b>More information</b></summary>\n\n <details>\n <summary><b>Sources I looked at before creating the custom class</b></summary>\n\n - [Return value from thread](https://stackoverflow.com/questions/1886090/return-value-from-thread)\n - [Threading in python: retrieve return value when using target= [duplicate]](https://stackoverflow.com/questions/2577233/threading-in-python-retrieve-return-value-when-using-target)\n - [How to get the return value from a thread in python?](https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python)\n - [Using Python Threading and Returning Multiple Results (Tutorial)](https://www.shanelynn.ie/using-python-threading-for-multiple-results-queue/)\n - [How to get the return value from a thread using python](https://www.edureka.co/community/31966/how-to-get-the-return-value-from-a-thread-using-python)\n - [How to manage python threads results?](https://stackoverflow.com/questions/3239617/how-to-manage-python-threads-results#3239815)\n - [How to obtain the results from a pool of threads in python?](https://stackoverflow.com/questions/26104512/how-to-obtain-the-results-from-a-pool-of-threads-in-python)\n - [Google search](https://www.google.com/search?hl=en&q=python%20save%20thread%20result)\n </details>\n</details>\n\n\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "Simple subclass wrapper around `threading.Thread` to get the return value from a thread in python. Exact same interface as `threading.Thread`! \ud83c\udf1f Star this repo if you found it useful! \ud83c\udf1f",
"version": "0.1.1.post1",
"project_urls": {
"Bug Reports": "https://github.com/slow-but-steady/save-thread-result/issues",
"Code": "https://github.com/slow-but-steady/save-thread-result/tree/main/python",
"Homepage": "https://github.com/slow-but-steady/save-thread-result/tree/main/python",
"Issues": "https://github.com/slow-but-steady/save-thread-result/issues",
"Pull requests": "https://github.com/slow-but-steady/save-thread-result/pulls",
"PyPi Funding": "https://donate.pypi.org",
"Source": "https://github.com/slow-but-steady/save-thread-result/tree/main/python"
},
"split_keywords": [
"threading",
"thread",
"multi-threading",
"logging",
"logger",
"archiving",
"tracing",
"tracer",
"debugging",
"debugger",
"automation",
"csv",
"txt",
"markdown",
"md",
"youtube",
"videos",
"url",
"scraping",
"selenium",
"macos",
"windows",
"linux"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "af6dfbf21b350439dc05bb7c8ed8fccd35f9be7186c1621cf8a08a6f1e2626bb",
"md5": "6a01e6f36da71f2c2200933a694f2b4c",
"sha256": "8f78b4eac2e49b0978c86d2a0d5465dda5ec2f47c94c202b323e57d74eca3a34"
},
"downloads": -1,
"filename": "save_thread_result-0.1.1.post1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6a01e6f36da71f2c2200933a694f2b4c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3, <4",
"size": 10306,
"upload_time": "2023-08-07T18:24:45",
"upload_time_iso_8601": "2023-08-07T18:24:45.090852Z",
"url": "https://files.pythonhosted.org/packages/af/6d/fbf21b350439dc05bb7c8ed8fccd35f9be7186c1621cf8a08a6f1e2626bb/save_thread_result-0.1.1.post1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6d45c05c254fb5e074f08a66882a9b0524509bf61a9c4d2b32409bf5ba13b84d",
"md5": "cad5e4bb9e679bbc4bd86418d623dc19",
"sha256": "46a214c1e204a8c4f92d293d0c121bb7bd38db98d79dc9b9384edce9dfbf0cbd"
},
"downloads": -1,
"filename": "save-thread-result-0.1.1.post1.tar.gz",
"has_sig": false,
"md5_digest": "cad5e4bb9e679bbc4bd86418d623dc19",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3, <4",
"size": 16693,
"upload_time": "2023-08-07T18:24:47",
"upload_time_iso_8601": "2023-08-07T18:24:47.603009Z",
"url": "https://files.pythonhosted.org/packages/6d/45/c05c254fb5e074f08a66882a9b0524509bf61a9c4d2b32409bf5ba13b84d/save-thread-result-0.1.1.post1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-07 18:24:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "slow-but-steady",
"github_project": "save-thread-result",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "save-thread-result"
}