This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow retrieval of return value from the target of a threading.Thread
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Joel Croteau2, pitrou, remi.lapeyre, vstinner
Priority: normal Keywords:

Created on 2019-04-25 00:22 by Joel Croteau2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg340815 - (view) Author: Joel Croteau (Joel Croteau2) Date: 2019-04-25 00:22
It would be nice if, after a threading.Thread has completed its run, it were possible to retrieve the return value of the target function. You can do this currently by setting a variable from your target or by subclassing Thread, but this should really be built in. My suggested changes:
* Add an attribute to Thread, retval, initially set to None, that contains the return value of the target after a successful completion.
* Thread.run() should set self.retval to the return value of the target upon completion, and also return this value.
* Thread.join() should return self.retval after a successful completion.

If you're not using Thread.join(), you can directly access Thread.retval to get the return result after a successful run. Thread.run() and Thread.join() both return None in all cases now, so I think a change in their return value would have minimal if any effect on existing code.
msg402682 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-09-26 21:21
Storing the result of Thread.run() can keep Python objects alive longer than expected and it may require a lock on it. I don't think that the additional complexity is worth it. I suggest to reorganize your code to pass the result differently to the caller thread. You can use a queue, a variable, whatever works.
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80898
2021-09-26 21:21:37vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg402682

resolution: wont fix
stage: resolved
2019-05-06 15:07:56remi.lapeyresetnosy: + remi.lapeyre
2019-04-25 01:06:41xtreaksetnosy: + pitrou
2019-04-25 00:22:54Joel Croteau2create