multithreading - How to stop all threads on KeyboardInterupt with Python's workerpool -
this job class:
class queryjob(workerpool.job): "job downloading given url." def __init__(self, query): self.query = query # query we'll need download when job runs def run(self): try: // query something... except (exception, keyboardinterrupt, systemexit): # todo: keyboardinterrupt not seem work... print '*** shutting down ***' pool.shutdown() pool.wait()
this how start it:
# initialize pool, 12 threads in case pool = workerpool.workerpool(size=12) # loop on input file , create job download url on each line query in open(options.file): job = queryjob(query) pool.put(job)
if i'd stop before it's finished, hit ctrl-c, nothing happens. try ctrl-c repeatedly no avail. finally, i'll ctrl-z , find process id , kill -9
stop threads.
is want it? there no way catch keyboardinterrupt i'm trying above?
note, i've tried other things in except
sys.exit()
, raise
. seems it's not reaching point , ctrl-c has no affect @ once threads executing.
is there trivial i'm missing?
thanks.
i found this: http://code.activestate.com/recipes/577187-python-thread-pool/
it seems function workerpool does, listen keyboardinterrupt , halt script.
this works me, i'm answering own question it. i'm still finding way use workerpool, else same situation - in meantime recommend using python thread module done in above recipe.
Comments
Post a Comment