dhanlin
dhanlin

Reputation: 145

SIGINT signal handler behaviour difference in python 2.7 vs 3.7

import signal

def handler(signum, frame):
    exit(0)

signal.signal(signal.SIGINT, handler)

str = 'abc'*1000000000
print(str)
print("done")

the above give code when run with python3.7, stops immediately on pressing ctrl+C , but when run with python2.7 takes multiple key strokes of ctrl+C.

If there is a valid reason for this behaviour please let me know.

Upvotes: 0

Views: 439

Answers (1)

Basil Crow
Basil Crow

Reputation: 316

Yes, there is a valid reason for this behavior. The behavior of time.sleep has changed between Python 2.7 and Python 3.7.

From the Python 2.7 documentation:

The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine.

From the Python 3.7 documentation:

Changed in version 3.5: The function now sleeps at least secs even if the sleep is interrupted by a signal, except if the signal handler raises an exception (see PEP 475 for the rationale).

Upvotes: 1

Related Questions