How to cleanly pause/stop a long running function?

Basilisk96 basilisk96 at gmail.com
Sat May 12 16:51:05 EDT 2007


Suppose I have a function that may run for a long time - perhaps from
several minutes to several hours. An example would be this file
processing function:

import os
def processFiles(startDir):
    for root, dirs, files in os.walk(startDir):
        for fname in files:
            if fname.lower().endswith(".zip"):
                # ... do interesting stuff with the file here ...

Imagine that there are thousands of files to process. This could take
a while. How can I implement this so that the caller can pause or
interrupt this function, and resume its program flow? Doing a Ctrl+C
interrupt would be a not-so-clean-way of performing such a thing, and
it would quit the application altogether. I'd rather have the function
return a status object of what it has accomplished thus far.

I have heard about threads, queues, and asynchronous programming, but
am not sure which is appropriate for this and how to apply it. Perhaps
the above function should be a method of a class that inherits from
the appropriate handler class? Any help will be appreciated.

-Basilisk96




More information about the Python-list mailing list