How to cleanly pause/stop a long running function?

Adam Atlas adam at atlas.st
Sat May 12 21:02:29 EDT 2007


On May 12, 4:51 pm, Basilisk96 <basilis... at gmail.com> wrote:
> 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

Consider using generators.
http://docs.python.org/tut/node11.html#SECTION00111000000000000000000

This way, whatever part of your program calls this function can
completely control the iteration. Maybe you can have it yield status
information each time.




More information about the Python-list mailing list