Intermittently check how long a program has been running and offer the user a chance to cancel tasks that are taking too long.
strftime()
directives.The datetime.datetime.strptime(
time_string
, format
)
function returns a datetime
object of the moment specified by time_string
, parsed using the format
string argument. See for the format details.
Rather than having all of your code wait until the time.sleep()
function finishes, you can execute the delayed or scheduled code in a separate thread using Python’s threading
module. The separate thread will pause for the time.sleep
calls. Meanwhile, your program can do other work in the original thread.
To make a separate thread, you first need to make a Thread
object by calling the threading.Thread()
function. Enter the following code in a new file and save it as threadDemo.py:
A multithreaded program that has some threads downloading comics while others are establishing connections and writing the comic image files to disk uses your Internet connection more efficiently and downloads the collection of comics more quickly. Open a new file editor window and save it as multidownloadXkcd.py. You will modify this program to add multithreading. The completely modified source code is available to download from .
threading
module and making a downloadXkcd()
function, which takes starting and ending comic numbers as parameters.For example, calling downloadXkcd(140, 280)
would loop over the downloading code to download the comics at , , , and so on, up to . Each thread that you create will call downloadXkcd()
and pass a different range of comics to download.
Add the following code to your multidownloadXkcd.py program:
for an example of multiple calculator processes open at once.Every process can have multiple threads. Unlike threads, a process cannot directly read and write another process’s variables. If you think of a multithreaded program as having multiple fingers following source code, then having multiple processes of the same program open is like having a friend with a separate copy of the program’s source code. You are both independently executing the same program.
If you want to start an external program from your Python script, pass the program’s filename to subprocess.Popen()
. (On Windows, right-click the application’s Start menu item and select Properties to view the application’s .
The return value is a Popen
object, which has two useful methods: poll()
and wait()
.
You can think of the poll()
method as asking your friend if she’s finished running the code you gave her. The poll()
method will return None
if the process is still running at the time poll()
is called. If the program has terminated, it will return the process’s integer exit code. An exit code is used .
Using your operating system’s built-in scheduler saves you from writing your own clock-checking code to schedule your programs. However, use the time.sleep()
function if you just need your program to pause briefly. Or instead of using the operating system’s scheduler, your code can loop until a certain date and time, calling time.sleep(1)
each time through the loop.
Add the following to your code:
can tell you how to use the scheduler already provided by your operating system.The threading
module is used to create multiple threads, which is useful when you need to download multiple files or do other tasks simultaneously. But make sure the thread reads and writes only local variables, or you might run into concurrency issues.
Finally, your Python programs can launch other applications with the subprocess.Popen()
function. Command line arguments can be passed to the Popen()
call to open specific documents with the application. Alternatively, you can use the start
, open
, or see
program with Popen()
to use your computer’s file associations to automatically figure out which application to use to open a document. By using the other applications on your computer, your Python programs can leverage their capabilities for your automation needs.