Python Forum
Get return value from a threaded function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get return value from a threaded function
#3
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).
Reply


Messages In This Thread
RE: Get return value from a threaded function - by supuflounder - Mar-12-2021, 01:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Multi-Threaded Camera Feed issue Khajababa69 0 1,590 May-05-2024, 09:44 PM
Last Post: Khajababa69
  nested function return MHGhonaim 2 1,707 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 5,993 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 2,187 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 2,857 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Improves performance on socket multi-threaded servers for object detection pennant 1 2,768 Aug-31-2021, 08:43 AM
Last Post: Larz60+
  How to invoke a function with return statement in list comprehension? maiya 4 4,204 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 6,814 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Return not exiting function?? rudihammad 3 7,601 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Why does my function return None? vg100h 3 3,346 Oct-29-2020, 06:17 AM
Last Post: vg100h

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020