HELP! How to return the returned value from a threaded function

MRAB python at mrabarnett.plus.com
Sat Apr 18 23:01:25 CEST 2015


On 2015-04-18 18:50, D. Xenakis wrote:
>> This sounds like homework... what have you tried, and what happened?
>
> heheh naaah no cheating here. I just provided the example this way to make as clear as possible what I want to do. Return the returned value from a threaded function.
>
> apparently this does not work as I hoped:
> return Thread(target=message_function).start()
>
>> Have you looked at the docs of the threading module?
> Lost in there
>
It would be pointless to start a thread to do some work and then wait
for it to finish; you might as well do the work yourself!

The point of threads is to do work in parallel (actually, that doesn't
work that well on CPython because of the GIL) or wait for something
that could take a while to respond in the background so that you can
continue working on something else in the meantime.

The best way of communicating with another thread is via queues: the
background thread puts its results into a queue that the main thread
can then read.



More information about the Python-list mailing list