如何使用 Python 的工作池停止 KeyboardInterupt 上的所有线程

发布于 2024-11-25 08:44:27 字数 1125 浏览 0 评论 0原文

这是我的作业类:

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()

这就是我启动它的方式:

# 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)

如果我希望它在完成之前停止,我按 Ctrl-C,但什么也没有发生。然后我反复尝试 Ctrl-C 也无济于事。最后,我将执行 Ctrl-Z,然后找到进程 ID 并执行 kill -9 来停止所有线程。

这是唯一想做的事情吗?有没有办法像我上面尝试做的那样真正捕获键盘中断?

请注意,我在 except 中尝试了其他操作,例如 sys.exit()raise。但似乎它甚至还没有达到这一点,并且一旦线程执行,Ctrl-C 就根本没有任何影响。

我缺少什么微不足道的东西吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-12-02 08:44:27

我发现了这个:
http://code.activestate.com/recipes/577187-python-thread- pool/

它的功能似乎与workerpool一样,但实际上会监听键盘中断并停止脚本。

这对我有用,所以我用它来回答我自己的问题。我仍然在寻找一种使用workerpool的方法,但对于其他有同样情况的人来说——同时我建议使用Python线程模块,就像上面的食谱中所做的那样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文