信号处理程序和Python中的日志记录

信号处理程序和Python中的日志记录

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

问题描述

记录模块的文档表示

这表明不应直接或间接从信号处理程序调用的代码进行日志记录调用.如果您不时执行一次,则程序将仅处于杀死-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中的信号会延迟到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中的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:53