Python threading thread wait

Wait for a Result from a Thread in Python

You can wait on results from a new thread in a number of ways.

Common approaches include, using a sleep, joining the new thread, using an event or wait/notify to signal that results are available, and to share results using a thread-safe queue.

In this tutorial you will discover how to wait for a result from a thread in Python.

Need to Wait for Result from a Thread

A thread is a thread of execution in a computer program.

Every Python program has at least one thread of execution called the main thread. Both processes and threads are created and managed by the underlying operating system.

Sometimes we may need to create additional threads in our program in order to execute code concurrently.

Python provides the ability to create and manage new threads via the threading module and the threading.Thread class.

You can learn more about Python threads in the guide:

In concurrent programming, we often may need for one thread to wait for a result from another thread.

For example, the main thread waiting on a result from a new thread.

This could be for many reasons, such as:

  • The new thread is performing a sub-task required by the main thread.
  • The new thread is retrieving data from an external resource like a file or socket.
  • The new thread is collating or calculating data.

How can one thread wait on a result from another thread?

Читайте также:  trigger demo

Run your loops using all CPUs, download my FREE book to learn how.

How to Wait for a Result from a Thread

It is common to use a new thread to perform a sub-task required by another thread.

This is often a task that involves blocking function calls, such as reading or writing a file, a socket, or similar external resource.

Python threads do not provide a direct method to get results from a new thread to a waiting thread. Instead indirect methods can be used, such as:

  • Use instance variables on an extended threading.Thread.
  • Use a queue.Queue to share a result between threads.
  • Use a global variable to share a result between threads.

You can learn more about how to return a result from a thread in this tutorial:

Before we can get the result from the new thread, the new thread must prepare the result. This means that the other thread must wait until the result is prepared and made available.

There are many ways for one to wait for a result from another thread.

Five common approaches include:

  1. Use a Sleep: Use a busy-wait loop with a call to time.sleep().
  2. Join the Thread: Call join() to wait for the new thread to terminate.
  3. Use an Event: Wait on a threading.Event to be set.
  4. Use a Wait/Notify: Wait on a threading.Condition to be notified about each result.
  5. Use a Queue: Wait on a queue.Queue for results to arrive.

Let’s take a closer look at each approach in turn.

Confused by the threading module API?
Download my FREE PDF cheat sheet

Wait for a Result With Sleep

We can wait for a result using sleep.

Specifically, the waiting thread can call the time.sleep() function and specify a number of seconds to wait.

Читайте также:  Оперативная система на java

The thread will then block until the number of seconds has elapsed, before checking whether the new thread has completed and returned a result.

This could be performed in a loop, where each iteration the waiting thread checks if the result is ready and available. If so, the thread can break the loop and use the result, otherwise it can repeat the waiting process.

Источник

Оцените статью