Software Development

What is the most reliable way to retrieve data from a child thread back to the main thread in Python?

SA Asked by Sarah Jenkins · 14-05-2024
0 upvotes 12,424 views 0 comments
The question

I'm using the threading module to run several API calls in parallel. I realized that thread.join() only waits for completion but doesn't actually return the function's result. Should I be using global variables with locks, or is there a built-in way to "yield" a result? I've heard about concurrent.futures and Queue, but I'm not sure which is considered the industry standard for clean, maintainable code.

3 answers

0
KI
Answered on 16-05-2024

The modern and most "Pythonic" way to handle this is using the concurrent.futures.ThreadPoolExecutor. Instead of manually managing threading.Thread objects, you submit your function to the executor, which returns a Future object. You can then call future.result(), which will block until the thread finishes and return the actual value (or raise an exception if one occurred). This is far superior to manual threading because it provides a clean interface for results and better resource management. For example, with ThreadPoolExecutor() as executor: future = executor.submit(my_func, arg). This keeps your code readable and avoids the common pitfalls of shared state management.

0
MA
Answered on 20-05-2024

If I have a long-running thread that needs to return multiple updates or "partial" results over time, would concurrent.futures still be the right choice, or should I look into something else?

DA 22-05-2024

Mark, for streaming data or multiple updates, a queue.Queue is much better suited than a Future. You can have the child thread use q.put(data) whenever it has a result, and the main thread can use q.get() to process them as they arrive. This creates a producer-consumer relationship that is thread-safe and prevents the main thread from idling while waiting for the entire process to finish, which is a common pattern in high-performance Software Development.

0
EM
Answered on 05-06-2024

You can also subclass threading.Thread and override the run method to store the result in an instance variable, then access it after calling join()

KI 07-06-2024

I agree with Emily; subclassing is a great way to understand what's happening under the hood. However, as Sarah (the OP) mentioned, for most production work, I'd stick to ThreadPoolExecutor because it handles the boilerplate for you and makes the code significantly easier for other team members to audit and maintain.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

World globe icon Country: Switzerland

Book Free Session