Skip to content

Instantly share code, notes, and snippets.

@ryanpetrello
Last active October 18, 2016 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanpetrello/6626ae4fe1e20ce651e3e83f677dc602 to your computer and use it in GitHub Desktop.
Save ryanpetrello/6626ae4fe1e20ce651e3e83f677dc602 to your computer and use it in GitHub Desktop.
Potential Python 3.5 signal.signal regression?
$ 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
@ryanpetrello
Copy link
Author

ryanpetrello commented Oct 18, 2016

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).

@ryanpetrello
Copy link
Author

ryanpetrello commented Oct 18, 2016

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

@ryanpetrello
Copy link
Author

ryanpetrello commented Oct 18, 2016

@ryanpetrello
Copy link
Author

Answer: this is an intentional change introduced in Python3.5:

https://www.python.org/dev/peps/pep-0475/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment