Updated June 16, 2023
Introduction to Python wait()
The wait() method in Python is defined as a method that enables a running process, such as a parent process, to wait for another process, such as a child process, to complete its execution before resuming the execution of the parent process or event. This wait()method in Python is a method of the os module that generally makes the parent process synchronize with its child process, which means the parent will wait for the child process to complete its execution (i.e, wait until the exit of the child process) and later continue with its process execution.
Working of wait() Method in Python
In Python, you can find the wait() function in two different modules: the os module and the threading module. Whereas the os module also has the same working, but in the os module, it works with the parent process to wait until the child completes its execution. In the below section, let us both these methods in detail with examples. In Python, the os.wait() function has syntax as follows:
This syntax returns the child process’s id in the form of a tuple and a 16-bit integer which is also present in the tuple to denote the exit status. This method returns a 16-bit integer which in turn includes higher and lower bytes where the lower byte has the signal number as zero, which will kill the process, and the higher byte will have the exit status notification. This os.wait() function does not take any parameters or arguments.
Examples of Python wait()
Now let us see a simple example of this method as follows:
import os print(" Program to demonstrate wait() method:") print("\n") print("Creating child process:") pr = os.fork() if pr is 0: print("Child process will print the numbers from the range 0 to 5") for i in range(0, 5): print("Child process printing the number %d"%(i)) print("Child process with number %d existing" %os.getpid()) print("The child process is",(os.getpid())) else: print("The parent process is now waiting") cpe = os.wait() print("Child process with number %d exited" % (cpe)) print("Parent process with number %d exiting after child has executed its process" % (os.getpid())) print("The parent process is", (os.getpid()))
In the above program, we can see to demonstrate the wait() method of the os module in Python. Firstly, the import os module. So when we want the wait() method for the parent process until the child process completes its execution, to invoke or create a child process, we have to call the fork() method. Then to get the parent id, we have to call the getpid() method. So in the above program, it will print the child process number ranging from 0 to 4, and until it prints the child process, the parent process will be waiting. It also again requires the lock and returns with the specified timeout.
Firstly we need to import the threading module and event class, and that can be done as follows;
from threading import Event wait( timeout= None)
The wait() method can take an optional argument called timeout, which is used to specify the specific time for waiting. After the specified time has elapsed, the events or threads will be unblocked. The method returns a Boolean value, where it will return True if the thread is released before the timeout, and it will return False if the timeout occurs and the thread is not released.
So let us see a sample example of how the wait() method works in the threading module.
import threading import time def hf(et_obj, timeout,n): print("Thread started, for the event to set") print("\n") flag = et_obj.wait(timeout) if flag: print("The Event earlier was true, now moving forward") else: print("Time has run out , yet the event internal flag is still false.") print("Start executing thread without waiting for event to become false") print(n) print("\n") if __name__ == '__main__': print("Start invoking the event") et_obj = threading.Event() t1 = threading.Thread(target=hf, args=(et_obj,5,17)) t1.start() time.sleep(5) print("It will start generating the event") print("\n") et_obj.set() print("So the Event is now set to true.") print("Now threads can be released.") print()
In the above program, we are demonstrating the wait() method with a timeout parameter where we are importing the thread module. First, the event will start by setting it to true. By making the thread sleep for 5 seconds, we can ensure that it resumes execution only after the specified time has passed.
In this article, we explored the os (operating system) module in Python, which offers the wait() function. This function allows the parent process to wait until its child process completes execution. We discussed the utilization of the wait() function in the context of the parent-child process relationship, and an example was provided to illustrate how it can be used.
This is a guide to Python wait(). Here we also discuss the introduction and working of the wait() method in Python with examples along with different examples and its code implementation. You may also have a look at the following articles to learn more –