信号处理程序和Python中的日志记录 [英] Signal handlers and logging in Python

查看:78
本文介绍了信号处理程序和Python中的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记录模块的文档表示

如果要使用信号模块实现异步信号处理程序,则可能无法使用此类处理程序中的日志记录.这是因为线程模块中的锁实现并不总是可重入的,因此不能从此类信号处理程序中调用.

If you are implementing asynchronous signal handlers using the signal module, you may not be able to use logging from within such handlers. This is because lock implementations in the threading module are not always re-entrant, and so cannot be invoked from such signal handlers.

这表明不应直接或间接从信号处理程序调用的代码进行日志记录调用.如果您不时执行一次,则程序将仅处于杀死-9的状态.

This suggests that one should not make logging calls from the code invoked by the signal handler directly or indirectly. If you do once in a while program will be left is a state when only kill -9 helps.

接下来是对我来说重要的问题.当主线程正在处理信号时,当其他线程调用日志记录方法时,也会发生这种锁定问题吗?

Important question for me now is following. Can this locking problem also happen when other threads call logging methods at the time when main thread is processing a signal?

推荐答案

信号处理程序在UNIX编程中完全需要特殊处理.只有已定义的POSIX C函数列表被声明为可重入的,并且可以在POSIX信号处理程序中调用. IEEE Std 1003.1列出了在 https://www.opengroup.org/上找到的118个可重入UNIX函数(需要登录) ).

Signal handlers need special handling in UNIX programming at all. Only a defined list of POSIX C functions are declared as reentrant and can be called within a POSIX signal handler. IEEE Std 1003.1 lists 118 reentrant UNIX functions you find at https://www.opengroup.org/ (login required).

但是Python信号是异步的:信号模块有一个说明:

But Python signals are asynchronous: The signal module have an clarification:

尽管Python信号处理程序是 异步调用 对于Python用户而言,他们可以 只发生在原子"之间 Python的说明 口译员.这意味着信号 经过长时间的计算 完全以C实现(例如 正则表达式匹配大 正文)可能会延迟 任意时间.

Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the "atomic" instructions of the Python interpreter. This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time.

在这种情况下,Python中的信号会延迟到GIL免费释放为止.

In this case, signals in Python are postponed until the GIL is free.

回到您的问题. ,只要您在信号处理功能中使用重入功能.如果仅在线程中使用日志记录,则不会有问题.

Back to your question. No, as long you use re-entrant functions within signal processing function. If logging is used in threads only, there will be no problems.

这篇关于信号处理程序和Python中的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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