How to Pause a Python Script and Resume Again

"

This article is part of in the series

Last Updated: Thursday thirtyth Dec 2021

Suppose y'all are developing a user interface, and yous support it with your code. Your user is uploading a document, and your lawmaking needs to await for the time the file is being uploaded. Also, when you visit a complex having automatic doors, you must have noticed that while you are entering the complex, the door stands still. But when you lot have entered the complex, the door closes automatically. What is causing the delay in both situations?

Codes support both systems discussed higher up. Information technology is the time delay office of programming languages that is causing the required time delay. We can too add time delays in our Python codes. The fourth dimension module of Python allows united states to plant delay commands between two statements. There are numerous means to add a time delay and, in this article, nosotros will discuss each method step-by-step.

Python'due south time.sleep() – Break, Stop, Wait or Sleep your Python Code

Python's time module has a handy function chosen slumber() . Essentially, as the name implies, it pauses your Python program. The time.sleep() command is the equivalent to the Fustigate shell'south sleep command. Almost all programming languages have this characteristic.

The time.slumber() function

While using this function, we can specify the delay, and the compiler will execute the fixed time delay. The syntax of this function is as follows:

time.sleep function 1

time.slumber() Arguments

  • secs - The number of seconds the Python program should suspension execution. This statement should be either an int or bladder .

Using Python'south time.sleep()

Hither's a quick, simple example of the syntax:

time.sleep function 2

Here we have instructed the arrangement to wait for v seconds through the start command and then look for 3 hundred milliseconds, which equals 0.three seconds. You can note hither that we have written the value of the time delay within the bracket based on the syntax.

At present let us consider another example to execute time delay.

time.sleep function 3

Here nosotros have taken a variable 'a' whose value we take stored as v. Now we are printing the value 'a' so again printing an increased value of 'a'. Nevertheless, at that place is a time delay between the execution of both statements. And, we have specified that using the fourth dimension.slumber() role. Y'all must have observed that we have also imported the time module at the beginning of the code.

Now let's see what the output is:

time.sleep function 4

Here nosotros can see that just the first control is executed. Now the output after five seconds:

time.sleep function 5

At present, after a delay of 5 seconds, the 2nd argument is besides executed.

Advanced syntax of the sleep() function

Hither'due south a more than advanced example. It takes user input and asks you how long you want to sleep() . It also proves how information technology works past printing out the timestamp earlier and subsequently the time.slumber() call. Note that Python 2.x uses the raw_input() office to get user input, whereas Python iii.x uses the input() role. Now let us meet the input syntax:

sleep function 1

The code given above asks the user how long to wait. We have mentioned the control for this output in the sleeper() function. Then, the code prints the calculator time at the beginning of the execution of code and after the implementation of code. This way, we get to see the bodily functioning of the delay part. Now allow's see the output:

sleep function 2

The organisation asks for our input, i.e., how long we desire the system to wait. Let'south enter v seconds and observe the final output.

sleep function 3

We can see that the starting computer fourth dimension("Earlier") and catastrophe computer time("After") have a fourth dimension difference of five seconds.

The Accuracy of time.sleep()

The time.sleep() function uses the underlying operating system'southward slumber() office. Ultimately there are limitations of this function. For example, on a standard Windows installation, the smallest interval you may filibuster is 10 - 13 milliseconds. The Linux kernels tend to take a higher tick rate, where the intervals are generally closer to one millisecond. Notation that in Linux, yous can install the RT_PREEMPT patch set, which allows yous to have a semi-realtime kernel. Using a existent-time kernel will further increase the accuracy of the fourth dimension.sleep() office. However, unless you desire to sleep for a brief period, you tin generally ignore this information.

Using Decorators To Add together time.sleep() Control

Decorators are used for creating simple syntax for calling college-guild functions. When can we use decorators? Suppose we have to test a function again, or the user has to download a file again, or you have to cheque the status of an interface subsequently a specific time interval. You require a fourth dimension delay between the offset attempt and the second attempt. Thus, you lot tin can utilize decorators in cases where you demand to check repeatedly and require a time delay.

Let's consider an example using decorators. In this programme, we will calculate the amount of time taken for the execution of the function.

time.sleep Command 1

Here time_calc() is the decorator office extending and containing the other functions, thus providing a simple syntax. Allow's see what nosotros get as the output.

time.sleep Command 2

Using Threads To Add together time.slumber() Command

Multiple-threading in Python is equivalent to running several programs simultaneously. When multi-threading is combined with the fourth dimension module, we tin can solve complex problems quickly. Multi-threading functions are available from the threading module.

Permit'south print the alphabet song using multi-threading and fourth dimension delay.

time.sleep Command 3

Hither i thread is being used to print the individual alphabets. There is a time delay of 0.5 seconds besides. When you run the program, you volition see that each line of the alphabet vocal is printed after a delay of 0.5 seconds. Let's see what the output is:

print the individual alphabets 1 print the individual alphabets 2

Practice Problem For Time Filibuster

  1. Printing ii statements with a time filibuster:

    In this problem, we volition impress two statements, the first one being " Hello everyone! "

    And the second one being " This is a practice problem for fourth dimension delay." We volition introduce a time delay of five seconds between these two statements. Let's come across the syntax of the code for this trouble:

    Printing two statements 1

    As you can encounter, the basic steps are straightforward. We first import the time module. Adjacent, we give the impress control for the commencement argument. We so innovate fourth dimension delay through time.sleep() function, so we requite the command for printing the following statement. Permit's come across what the output is:

    Printing two statements 2

    This image shows the outset output.

    Printing two statements 3

    Now, this output is obtained 5 seconds later on than the previous output.

  2. Programme to display date and time :

    Let's create a program where nosotros make a digital clock that also displays the date.

    The lawmaking for this chore is as follows:

    Program to display date and time 1

    Here we have given the command to display the appointment and fourth dimension. And so nosotros have added a fourth dimension delay of one 2nd and then that the time gets updated. The output for this program is as follows:

    Program to display date and time 2

Conclusion

Fourth dimension delay has immense application in user interfaces. It becomes essential for a developer to know how she can add time delay in her program. Nosotros hope that this article helped you in learning nigh the fourth dimension module of Python. At present you can add together fourth dimension delays in your code efficiently and increment the productivity of your code!

franksbegist.blogspot.com

Source: https://www.pythoncentral.io/pythons-time-sleep-pause-wait-sleep-stop-your-code/

0 Response to "How to Pause a Python Script and Resume Again"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel