Introduction to Python wait()
Python wait() method is defined as a method for making the running process to wait for the other process like child process to complete the execution and then resume the process of the parent class or event. This wait()method in Python is a method of os module which generally makes the parent process to 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. This method is also defined as a method of event class in the Python threading module to suspend the execution of the event where its internal flag is set to false which will suspend the current block or event for executing until the internal flag is set to true.
Working of wait() Method in Python
In this article, we will discuss the wait() method which is obtained in the os module in the Python programming language and is declared or defined as os.wait(). This os.wait() function is used for suspending or stopping the parent process until the child process is executed. This wait() function is usually used for waiting whenever the process needs something to happen where it will wait until the function returns true with some specified or declared conditions or modes.
In Python, the wait() function is defined in two different modules such as the os module and the threading module. In the threading module the event class provides this wait()method for holding the current thread execution for the event to be executed or completed. 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 along with a 16-bit integer which is also present in the tuple to denote the exit status. This method which 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 for 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. So 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. Therefore the child process id that has completed the execution of the process will be exited and then the parent process will exit.
Now we will see another module in Python that uses the wait() method is a threading module which mainly includes mechanisms that allow synchronization of threads, so to release the locks and then lock another block until the thread is executed we use this wait() method and it also again requires the lock and returns with the specified timeout. This method is mainly found in the event class of the threading module in Python. So the syntax can be written as below:
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)
wait() method will take an argument timeout which is optional and is used for specifying the particular time for waiting and after the time is out the events or threads get unblocked. This method returns the Boolean value where it returns true if the thread is released before the timeout or else it will return a false value.
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 and then the timeout will start once the timeout occurs then automatically flag will be set to false where it will start executing the thread without waiting for the event to complete as the time is run out and if we want to the thread to wait until the event completes then we are using sleep() method where we are making the thread to sleep for 5 seconds and then resume and once the event is completed it will set to true where the threads can be released for further execution.
In this article, python provides a module known as os (operating system) where it provides the wait() function for the parent process to wait until its child gets executed completely. In this article, we saw how the wait() function can be used for the parent process to wait with an example. In this article, we also there is another module threading which also provides a wait() function for the thread to wait until the events get executed with example.
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 –