gpt4 book ai didi

python - 信号库处理Python中哪些函数是可重入的

转载 作者:太空狗 更新时间:2023-10-30 00:12:08 24 4
gpt4 key购买 nike

讨论 Signal handlers and logging in Python我想到了 Python 中哪些函数是可重入的问题。

signal library提及:

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.

logging library 指出重入不是典型的:

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.

我有点困惑,因为信号库将 GIL(全局解释器锁)称为“.. 在原子指令之间......”。在这种情况下,只要 GIL 离开/解锁,信号就会被推迟和执行。一种信号队列。

这是有道理的,但延迟信号处理程序调用的函数是否可重入并不重要,因为它们不是在具有“可重入性”的真实 POSIX 信号处理程序中调用的"-限制:

Only a defined list of POSIX C functions are declared as re-entrant and can be called within a POSIX signal handler. IEEE Std 1003.1 lists 118 re-entrant UNIX functions you find at https://www.opengroup.org/ (login required).

最佳答案

我认为日志记录模块不可重入的原因是它使用threading.Lock(而不是RLock)来同步多个线程记录到同一个处理程序(因此消息不会交织在一起)。

这意味着如果已获取锁的日志记录调用被信号处理程序中断并且信号处理程序尝试记录它将永远死锁,等待先前的acquire被释放。

顺便说一句,这些锁与 GIL 无关,它们是“用户创建的”锁,换句话说,GIL 是解释器使用的锁(实现细节)。

关于python - 信号库处理Python中哪些函数是可重入的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4604634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com