2 ms·
Thank you for this. I did not know of this change in implementation in Python 3.8. Are you a core dev btw? I have a complaint about single threads and processe
by hnews_account_1 7y ago
Thank you for this. I did not know of this change in implementation in Python 3.8.
Are you a core dev btw? I have a complaint about single threads and processes and how they have absolutely no way to return values back to the main thread except through some shared memory object. Am I too ignorant to understand how big of a challenge it is to do this?
from threading import Thread
def func1(*args):
#something
def main():
new_thread, return_val = Thread(target=func1, args=(1, 2))
new_thread.start()
new_thread.join()
print(return_val)
Instead to implement this, I keep having to use like single process pools that already have return mechanisms encoded. All I need is for a way of starting off a thread (or process) and joining it once my main thread / process is done and retrieving its return value for my use (assuming there is one).
- BiteCode_dev 7y agoNot a core dev. The clean way to return a value is to pass it to a Queue (https://docs.python.org/fr/3/library/queue.html https://docs.python.org/fr/3/library/queue.html), and this is what the executor does, but for your use case, it's overkill. For very simple use cases, you can inherit from Thread and force join() to return the value: class SimpleReturningThread(Thread): result = None def run(self): try: if self._target: self.result = self._target(*self._args, **self._kwargs) finally: del self._target, self._args, self._kwargs def join(self, *args, **kwargs): super().join(*args, **kwargs) return self.result Personally, I'd stick to using a pool with only one worker in it. It's not worth the trouble of doing all this work. Wrap it in a few functions if you do the same thing repeatedly and call it a day, it's unlikely going to be what most your code is about anyway.