在 Python 中是否有推荐的方法来调试 Web 应用程序后端冻结的原因?
Is there a recommeded way to debug the cause of a freezing web application's backend in Python?
我的后端是用 Python 编写的,使用的是 CherryPy。我的一段代码处理来自前端的长轮询,如下所示:
# for the long-polls
longpoll_blockers_channel = {}
class MyApp:
...
@cherrypy.expose
@uses_json
def register_longpoll_blocker(self, classmodel_name):
if classmodel_name not in longpoll_blockers_channel:
unblock_event_obj = threading.Event()
unblock_event_obj.running_status = "long-polling"
longpoll_blockers_channel[classmodel_name] = unblock_event_obj
def unblocking_event_handler(mapper, connection, target):
longpoll_blockers_channel[classmodel_name].set()
listento_classmodel_obj = database.schemas_registry[classmodel_name]
# triggered by trio events:
for data_changing_evt in [
'after_delete', 'after_insert', 'after_update'
]:
sqlalchemy.event.listen(listento_classmodel_obj, data_changing_evt,
unblocking_event_handler)
return f"Successfully registered a blocker at backend for {classmodel_name}!"
我注意到上面的代码似乎只能处理最多 4 个长轮询注册线程。达到4个以上后,后端就卡住了。我怀疑冻结是由 threading.Event()
生成的线程过多引起的,但无法证明这一点。我想找出到底是什么导致后端挂起。
有很多 threading.Event()
线程可能会冻结应用程序吗?如何调试这样的错误?
经过几天的研究,我发现这个空闲是由于浏览器的blocking
机制,它只允许同时连接一定数量的并行连接。它与 Python 的后端和线程无关。
我的后端是用 Python 编写的,使用的是 CherryPy。我的一段代码处理来自前端的长轮询,如下所示:
# for the long-polls
longpoll_blockers_channel = {}
class MyApp:
...
@cherrypy.expose
@uses_json
def register_longpoll_blocker(self, classmodel_name):
if classmodel_name not in longpoll_blockers_channel:
unblock_event_obj = threading.Event()
unblock_event_obj.running_status = "long-polling"
longpoll_blockers_channel[classmodel_name] = unblock_event_obj
def unblocking_event_handler(mapper, connection, target):
longpoll_blockers_channel[classmodel_name].set()
listento_classmodel_obj = database.schemas_registry[classmodel_name]
# triggered by trio events:
for data_changing_evt in [
'after_delete', 'after_insert', 'after_update'
]:
sqlalchemy.event.listen(listento_classmodel_obj, data_changing_evt,
unblocking_event_handler)
return f"Successfully registered a blocker at backend for {classmodel_name}!"
我注意到上面的代码似乎只能处理最多 4 个长轮询注册线程。达到4个以上后,后端就卡住了。我怀疑冻结是由 threading.Event()
生成的线程过多引起的,但无法证明这一点。我想找出到底是什么导致后端挂起。
有很多 threading.Event()
线程可能会冻结应用程序吗?如何调试这样的错误?
经过几天的研究,我发现这个空闲是由于浏览器的blocking
机制,它只允许同时连接一定数量的并行连接。它与 Python 的后端和线程无关。