Mar-12-2021, 01:22 AM
Key here is that you have the wrong idea. You do not "return" a value from a thread. The thread runs, and eventually, terminates. There is no concept of "returning" anything.
However, you can ideally send a message between two threads, or have a shared queue into which you place results. Ultimately, the "main thread" can decide to examine this queue and find whatever elements you have put in it. I am not familiar enough with Python to say how this is done, but this is how I do it in other languages. Threads never "return" values because there is no place that they can return them to. But you can use shared resources, which you have to make sure are properly locked and synchronized, to pass information between threads. In some systems and libraries these capabilities are built in. I have not tried multithreading in Python, although I have depended on it for dozens of projects done in C/C++ and other languages (I've been using multithreading since about 1969).
However, you can ideally send a message between two threads, or have a shared queue into which you place results. Ultimately, the "main thread" can decide to examine this queue and find whatever elements you have put in it. I am not familiar enough with Python to say how this is done, but this is how I do it in other languages. Threads never "return" values because there is no place that they can return them to. But you can use shared resources, which you have to make sure are properly locked and synchronized, to pass information between threads. In some systems and libraries these capabilities are built in. I have not tried multithreading in Python, although I have depended on it for dozens of projects done in C/C++ and other languages (I've been using multithreading since about 1969).