Last active
October 18, 2016 03:18
-
-
Save ryanpetrello/6626ae4fe1e20ce651e3e83f677dc602 to your computer and use it in GitHub Desktop.
Potential Python 3.5 signal.signal regression?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python2 --version && python3.4 --version && python3.5 --version | |
Python 2.7.11 | |
Python 3.4.4 | |
Python 3.5.1 | |
$ cat alarm.py | |
import signal | |
import time | |
def receive_alarm(signum, stack): | |
print('Alarm :', time.ctime()) | |
# Call receive_alarm in 2 seconds | |
signal.signal(signal.SIGALRM, receive_alarm) | |
signal.alarm(2) | |
print('Before:', time.ctime()) | |
time.sleep(8) | |
print('After :', time.ctime()) | |
$ time python2 alarm.py | |
('Before:', 'Mon Oct 17 22:15:15 2016') | |
('Alarm :', 'Mon Oct 17 22:15:17 2016') | |
('After :', 'Mon Oct 17 22:15:17 2016') | |
python2 alarm.py 0.04s user 0.02s system 2% cpu 2.075 total | |
$ time python3.4 alarm.py | |
Before: Mon Oct 17 22:24:08 2016 | |
Alarm : Mon Oct 17 22:24:10 2016 | |
After : Mon Oct 17 22:24:10 2016 | |
python3.4 alarm.py 0.10s user 0.02s system 5% cpu 2.133 total | |
$ time python3.5 alarm.py | |
Before: Mon Oct 17 22:15:21 2016 | |
Alarm : Mon Oct 17 22:15:23 2016 | |
After : Mon Oct 17 22:15:29 2016 | |
python3 alarm.py 0.08s user 0.01s system 1% cpu 8.107 total |
I'm using OS X here, but it does not seem OSX-specific; when I run the same code in Ubuntu 14.04, 2.7 and 3.4 have the same execution time (~2 seconds), but 3.5 takes the full 8 seconds:
$ python2 --version && python3.4 --version && python3.5 --version
Python 2.7.6
Python 3.4.0
Python 3.5.2
Answer: this is an intentional change introduced in Python3.5:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I encountered some really confusing behavior with Python on OSX when using
signal.signal(signal.SIGALRM)
. Specifically, the same Python code behaves quite differently between the versions of Python I have installed (2.7.11, 3.4.4, and 3.5.1).