Обложка канала

Библиотека Python разработчика

20835 @BookPython

Библиотека Python разработчика. Книги по программированию на Python.

Библиотека Python разработчика

4 года назад
Открыть в
A lot of system calls can be interrupted by an incoming signal. If a programmer wants the call to be completed anyway, they have to issue it again. The notable example is sleep(x) function that is expected to freeze the program for x seconds, but in reality, it can return earlier if a signal appears. However, since Python 3.5, thanks to PEP 475, Python cares about all such calls for you. The following program ends on the first SIGINT it receives in any Python before 3.5. But it sleeps for exactly five seconds regardless of the signals in Python 3.5+. import signal import time def signal_handler(signal, frame): print('Caught')a signal.signal(signal.SIGINT, signal_handler) time.sleep(5)