python 对信号 处理的测试
小结下:
每次信号 会将当前执行的函数挂起,进入 信号处理函数
如果信号处理函数还在处理,又来信号,当前函数仍然被挂起
执行完毕回到刚才挂起点继续执行
从下面输出 我们就可以看出来
^Cget an signal: 2 9- ^Cget an signal: 2 10
- ^Cget an signal: 2 11
- ^Cget an signal: 2 12
- ec 12 t: 12
- ec 12 t: 11
- ec 12 t: 10
- ec 12 t: 9
- #coding:utf-8
- '''''
- python 对linux 中断机制的测试
- 输出: 按ctrl+c 效果
- python test_sig.py
- . 0
- . 0 . 1
- . 1 . 2
- . 2 . 3
- . 3 . 4
- . 4 . 5
- . 5 . 6
- . 6 . 7
- . 7 . 8
- . 8 . 9
- . 9 . 10
- . 10 . 11
- . 11 . 12
- . 12 . 13
- . 13 . 14
- . 14 . 15
- . 15 . 16
- . 16 . 17
- . 17 . 18
- . 18 . 19
- . 19 . 20
- . 20 . 21
- . 21 . 22
- . 22 . 23
- . 23 . 24
- . 24 . 25
- . 25 . 26
- ^Cget an signal: 2 1
- ^Cget an signal: 2 2
- ^Cget an signal: 2 3
- ^Cget an signal: 2 4
- ^Cget an signal: 2 5
- ^Cget an signal: 2 6
- ^Cget an signal: 2 7
- ^Cget an signal: 2 8
- ^Cget an signal: 2 9
- ^Cget an signal: 2 10
- ^Cget an signal: 2 11
- ^Cget an signal: 2 12
- ec 12 t: 12
- ec 12 t: 11
- ec 12 t: 10
- ec 12 t: 9
- ec 12 t: 8
- ec 12 t: 7
- ec 12 t: 6
- ec 12 t: 5
- ec 12 t: 4
- ec 12 t: 3
- ec 12 t: 2
- ec 12 t: 1
- . 26 . 27
-
- main
-
- '''
- import sys
- import signal
- e={'flag':0,'c':0}
-
- def handler(signum, frame):
- e['c']+=1#计数器
- print "get an signal:", signum,e['c']
- import time
- t=e['c']
- time.sleep(2)
- print 'ec', e['c'],'t:',t
- e['flag']=1
-
-
- #开关
- signal.signal(signal.SIGINT, handler)
- signal.signal(signal.SIGTERM, handler)
- signal.signal(3, handler)
-
- def main():
- while 1:
- if e['flag']:break;
- for k in range(100):
- print '.',k
- if e['flag']:break;
- for i in range(100000):
- if e['flag']:break;
- s=i*i
- print '.',k,
- sys.stdout.flush()
-
- print
- print 'main'
-
-
- main()