Try running the code above in your terminal. You use KeyboardInterrupt to catch the user pressing Ctrl + C. Next, you start both threads and initiate a loop to log from the main thread every so often. You also create a logging object that will log the threadName to stdout. Here, you use Python’s threading module to create two threads. sleep ( 0.75 ) except KeyboardInterrupt : info = True logging. debug ( "Checking in from main thread" ) time. Thread ( target = worker, args = ( info ,)) thread. Thread ( target = worker, args = ( info ,)) thread_two = threading. HTTPError as e : # Email admin / log print ( f 'HTTPError: thread = threading. Import time import urllib.request import urllib.error def uptime_bot ( url ): while True : try : conn = urllib. If that sounds like you, then let’s get started! This article is intended for intermediate developers who are looking to grow their knowledge of Python. In this tutorial, you’ll learn how to add Python sleep() calls with: Adding Python sleep() calls to your program can help in each of these cases, and many more! You might even need to pause between calls to a web API, or between queries to a database. Perhaps you need to wait for a file to upload or download, or for a graphic to load or be drawn to the screen. But there are times when letting your code sleep for a while is actually in your best interest.įor example, you might use a Python sleep() call to simulate a delay in your program. Have you ever needed to make your Python program wait for something? Most of the time, you’d want your code to execute as quickly as possible. Watch it together with the written tutorial to deepen your understanding: Using sleep() to Code a Python Uptime Bot Watch Now This tutorial has a related video course created by the Real Python team.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |