Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5c239e

Browse files
committed
update
1 parent 3e17105 commit a5c239e

16 files changed

+723
-101
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
183183

184184
## [csv](content/csv.md)
185185

186-
## [][signal](content/signal.md)
186+
## [signal](content/signal.md)
187187

188188
## [asyncore](content/asyncore.md)
189189

‎code/signal_alarm.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import time
4+
import signal
5+
6+
7+
# After finish alarm
8+
def receive_alarm(signum, stack):
9+
print 'Alarm :', time.ctime()
10+
11+
12+
# Call receive_alarm with 2 seconds
13+
signal.signal(signal.SIGALRM, receive_alarm)
14+
print 'Before:', time.ctime()
15+
16+
# Unix only
17+
signal.alarm(2)
18+
print 'After alarm :', time.ctime()
19+
20+
# Alarm will finish sleep
21+
time.sleep(3)
22+
print 'After sleep :', time.ctime()

‎code/signal_demo.py‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
# coding=utf-8
1+
# -*- coding: utf-8 -*-
22

3-
import time
43
import signal
4+
import os
5+
import time
6+
57

6-
def handle(signo, frame):
7-
print"Got Signal",signo
8+
def receive_signal(signum, stack):
9+
print'Received:', signum
810

9-
signal.signal(signal.SIGINT, handle)
1011

11-
# Unix Only
12-
signal.alarm(2)
12+
if __name__ == '__main__':
1313

14-
now = time.now()
14+
# Register signal handlers
15+
signal.signal(signal.SIGUSR1, receive_signal)
16+
signal.signal(signal.SIGUSR2, receive_signal)
1517

16-
time.sleep(200)
18+
# Print the process ID so it can be used with 'kill'
19+
# to send this program signals.
20+
print 'My PID is:', os.getpid()
1721

18-
print "sleep for",time.time() - now," seconds "
22+
while True:
23+
print 'Waiting...'
24+
time.sleep(3)

‎code/signal_exit.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import signal
4+
import os
5+
6+
7+
def do_exit(sig, stack):
8+
raise SystemExit('Exiting')
9+
10+
11+
signal.signal(signal.SIGINT, signal.SIG_IGN)
12+
signal.signal(signal.SIGUSR1, do_exit)
13+
14+
print 'My PID:', os.getpid()
15+
16+
signal.pause() # 当接受到键盘的时候,signal.SIG_IGN给忽略了
17+
print 'End of Exit'

‎code/signal_official.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import signal
5+
6+
# 定义一个信号处理函数,该函数打印收到的信号,然后raise IOError
7+
def handler(signum, frame):
8+
print 'Signal handler called with signal', signum
9+
raise IOError("Couldn't open device!")
10+
11+
# 对SIGALRM(终止)设置处理的handler, 然后设置定时器,5秒后触发SIGALRM信号
12+
signal.signal(signal.SIGALRM, handler)
13+
signal.alarm(5)
14+
15+
# open 操作可能异常等待
16+
fd = os.open('/dev/ttys0', os.O_RDWR)
17+
18+
signal.alarm(0) # 关闭定时器

‎code/signal_show.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import signal
4+
import os
5+
import time
6+
7+
8+
def receive_signal(signum, stack):
9+
print 'Received:', signum
10+
11+
12+
def receive_exit(signum, stack):
13+
print 'Exit'
14+
raise SystemExit('Exiting')
15+
16+
17+
if __name__ == '__main__':
18+
19+
# Register signal handlers
20+
signal.signal(signal.SIGINT, receive_signal)
21+
signal.signal(signal.SIGQUIT, receive_signal)
22+
23+
# SIGSTOP, SIGKILL can't received message
24+
# signal.signal(signal.SIGSTOP, receive_signal)
25+
# signal.signal(signal.SIGKILL, receive_exit)
26+
27+
print 'My PID is:', os.getpid()
28+
29+
while True:
30+
print 'Waiting...'
31+
time.sleep(3)

‎code/signal_timeout.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import signal
4+
import requests
5+
6+
7+
def timeoutFn(func, args=(), kwargs={}, timeout_duration=5, default=None):
8+
9+
class TimeoutError(Exception):
10+
pass
11+
12+
def handler(signum, frame):
13+
raise TimeoutError()
14+
15+
# set the timeout handler
16+
signal.signal(signal.SIGALRM, handler)
17+
signal.alarm(timeout_duration)
18+
try:
19+
result = func(*args, **kwargs)
20+
except TimeoutError as exc:
21+
result = default
22+
finally:
23+
24+
signal.alarm(0)
25+
signal.signal(signal.SIGALRM, signal.SIG_DFL)
26+
return result
27+
28+
29+
if __name__ == '__main__':
30+
print timeoutFn(requests.get, ("https://baidu.com", ), default='failed')
31+
print timeoutFn(requests.get, ("https://google.com", ), default='failed')

‎code/signal_traverse.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import signal
4+
5+
6+
def alarm_received(n, stack):
7+
return
8+
9+
10+
signal.signal(signal.SIGALRM, alarm_received)
11+
12+
13+
# SIG_ING 如果信号被忽略
14+
# SIG_DFL 如果使用默认行为
15+
# SIG_IGN -- if the signal is being ignored
16+
# SIG_DFL -- if the default action for the signal is in effect
17+
# None -- if an unknown handler is in effect
18+
# anything else -- the callable Python object used as a handler
19+
signals_to_names = dict(
20+
(getattr(signal, n), n)
21+
for n in dir(signal)
22+
if n.startswith('SIG') and '_' not in n
23+
)
24+
25+
for s, name in sorted(signals_to_names.items()):
26+
handler = signal.getsignal(s)
27+
if handler is signal.SIG_DFL:
28+
handler = 'SIG_DFL'
29+
elif handler is signal.SIG_IGN:
30+
handler = 'SIG_IGN'
31+
print '%-10s (%2d):' % (name, s), handler

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /