在gevent应用程序中,如何杀死所有已启动的greenlet? [英] In a gevent application, how can I kill all greenlets that have been started?

查看:157
本文介绍了在gevent应用程序中,如何杀死所有已启动的greenlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gevent应用程序,它会在多个模块之间生成多个greenlet.我希望能够正常关闭应用程序(例如在内部或通过捕获SIGTERM),从而允许greenlet通过捕获GreenletExit并执行finally:子句很好地终止.

I have a gevent application that spawns multiple greenlets across multiple modules. I want to be able to gracefully shutdown the application (either internally or by catching SIGTERM, for instance), allowing greenlets to terminate nicely by catching GreenletExit and executing finally: clauses.

如果我拥有所有正在运行的greenlets中的a,则可以执行gevent.killall(list_of_greenlets),但是维护这样的列表相当麻烦;此外,gevent必须以某种形式保留此列表.

If I had the a of all running greenlets, I could do gevent.killall(list_of_greenlets), but maintaining such a list is rather a hassle; besides, gevent must be keeping this very list in some form or another.

那么,我可以杀死所有已经启动的greenlet而不维护它们的列表吗?

So, can I kill all greenlets that have been started without maintaining a list of them?

(我在raspbian上的python 2.7上使用gevent 1.0.0)

(I'm using gevent 1.0.0 on python 2.7 on raspbian)

推荐答案

根据

According to another SO answer, it's possible "to iterate through all the objects on the heap and search for greenlets." So, I imagine this ought to work:

import gc
import gevent
from greenlet import greenlet    
gevent.killall([obj for obj in gc.get_objects() if isinstance(obj, greenlet)])

这篇关于在gevent应用程序中,如何杀死所有已启动的greenlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆