Trade Off

supercalifragilisticexpialidocious

Two Words

看到python捕获SIGINT event的文档,上面说signal这个包里的东西的功能是set handler for asynchronous event,于是背了下这两个词——asynchronous和synchronous,简称分别是async和sync,由于这两个词比较长,我们通常都用简称,而且发音也都正好不会破坏。

asynchronous: 美 [eɪ'sɪŋkrənəs]
synchronous:  美 ['sɪŋkrənəs]

以上音标来自dict.cn

Here is a minimal example program. It uses the alarm() function to limit the time spent waiting to open a file; this is useful if the file is for a serial device that may not be turned on, which would normally cause the os.open() to hang indefinitely. The solution is to set a 5-second alarm before opening the file; if the operation takes too long, the alarm signal will be sent, and the handler raises an exception.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm

Comments