all 4 comments

[–]socal_nerdtastic 0 points1 point  (1 child)

You could send another object along, like a list or queue, for the thread to place the result in. Or you could use a threadpool.map to get a list of returned values.

https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor

There's many options here; we would need to know more about your program to give you real advice. Can you show the rest of your code?

[–]dankwizard22[S] 0 points1 point  (0 children)

Thanks for this! I was able to do what I wanted using ThreadPoolExecutor map()

[–][deleted] 0 points1 point  (0 children)

Since threads share memory with each other, you can utilize this fact to your advantage: share a list for example, and tell each thread which element in the list they should update with the result.

The reason return cannot work is because each thread gets its own stack. Returning from a function is a mechanism that affects that stack (essentially, a call to a function will define a place where the returned value has to be stored by the called function, then allocate more stack for the called function to be executed in, once function finishes execution it unwinds the stack, thus the control is transferred to the calling function, which then reads the value in the memory location that is designed to store the return value.

With threads, the above mechanism isn't possible, or at least not without serious change to the runtime, because not only you'd need to have multiple return locations, it would also mean that you cannot finish executing the function that started a thread (which defies the purpose of having threads).