Killing a running thread

Alex Martelli aleax at aleax.it
Thu May 2 11:32:48 EDT 2002


Alexander Skwar wrote:
        ...
> I'd do this, I wouldn't know how I could jump out of this function
> without iterating over the rest of the elements.

class OuttaHere(Exception): pass

then in your function
        raise Outtahere
when you want out, and in its callpoint:

try:
    map(myfunction, somehumongoushugelist)
except OuttaHere:
    pass

> from your link.  And finally I'm spending a large chunk of time in
> md5s.write(result_str_md5) (md5s is a file object).  For this I would
> not know how I could quit the thread at all.

That depends.  If you need the write to md5s to complete, and it's
taking a long time, then you cannot of course quit this thread at
all (if you COULD kill it, the file wouldn't be complete).  If it's
OK for the file to NOT be complete, change the single write call to,
e.g.:

base = 0
stride = 8192
while base < len(result_str_md5):
    nd5s.write(result_str_md5[base:base+stride])
    base += stride
    if mustExitNowAtAllCosts():
        "close and erase the file or whatever, then"
        break


Alex
   



More information about the Python-list mailing list