This is my Job class:
class QueryJob(workerpool.Job):
"Job for downloading a given URL."
def __init__(self, query):
self.query = query # The query we'll need to download when the job runs
def run(self):
try:
// Query something...
except (Exception, KeyboardInterrupt, SystemExit):
# TODO: The KeyboardInterrupt does not seem to work...
print '*** shutting down ***'
pool.shutdown()
pool.wait()
This is how I start it:
# Initialize a pool, 12 threads in this case
pool = workerpool.WorkerPool(size=12)
# Loop over input file and create a job to download the URL on each line
for query in open(options.file):
job = QueryJob(query)
pool.put(job)
If I'd like it to stop before it's finished, I hit Ctrl-C, but nothing happens. I then try Ctrl-C repeatedly also to no avail. Finally, I'll do Ctrl-Z and then find the process id and do a kill -9
to stop all the threads.
Is this the only want to do it? Is there no way to actually catch the KeyboardInterrupt like I'm trying to do above?
Note, I've tried other things in the except
like sys.exit()
and raise
. But it seems like it's not even reaching that point and Ctrl-C has no affect at all once the threads are executing.
Is there something trivial that I'm开发者_开发知识库 missing?
Thanks.
I found this: http://code.activestate.com/recipes/577187-python-thread-pool/
It seems to function just as workerpool does, but actually will listen to the KeyboardInterrupt and halt the script.
This works for me, so I'm answering my own question with it. I'm still up for finding a way to use workerpool, but for anybody else with this same situation - in the meantime I recommend using the Python thread module as is done in the above recipe.
精彩评论