作用:发送和接收异步系统信号

信号是一个操作系统特性,它提供了一个途径可以通知程序发生了一个事件并异步处理这个事件。信号可以由系统本身生成,也可以从一个进程发送到另一个进程。

由于信号会中断程序的正常控制流,如果在中间接收到信号,有些操作(特别是I/O操作)可能会发生错误。

接收信号:

signal.signal(sig,action)

sig为某个信号,action为该信号的处理函数。

例如:

signal.signal(signal.SIGALRM, hanlder)       hanlder为信号处理函数

windows下sig信号:

>>>dir(signal)

['CTRL_BREAK_EVENT', 'CTRL_C_EVENT', 'NSIG', 'SIGABRT', 'SIGBREAK', 'SIGFPE',

'SIGILL', 'SIGINT', 'SIGSEGV', 'SIGTERM', 'SIG_DFL', 'SIG_IGN', '__doc__', '__name__', '__package__',

'default_int_handler', 'getsignal', 'set_wakeup_fd', 'signal']

linux下sig信号:

>>>dir(signal)

['ITIMER_PROF', 'ITIMER_REAL', 'ITIMER_VIRTUAL', 'ItimerError', 'NSIG', 'SIGABRT', 'SIGALRM', 'SIGBUS', 'SIGCHLD',

'SIGCLD', 'SIGCONT', 'SIGFPE', 'SIGHUP', 'SIGILL', 'SIGINT', 'SIGIO', 'SIGIOT', 'SIGKILL', 'SIGPIPE', 'SIGPOLL',

'SIGPROF', 'SIGPWR', 'SIGQUIT', 'SIGRTMAX', 'SIGRTMIN', 'SIGSEGV', 'SIGSTOP', 'SIGSYS', 'SIGTERM', 'SIGTRAP',

'SIGTSTP', 'SIGTTIN', 'SIGTTOU', 'SIGURG', 'SIGUSR1', 'SIGUSR2', 'SIGVTALRM', 'SIGWINCH', 'SIGXCPU', 'SIGXFSZ',

'SIG_DFL', 'SIG_IGN', '__doc__', '__name__', '__package__', 'alarm', 'default_int_handler', 'getitimer', 'getsignal',

'pause', 'set_wakeup_fd', 'setitimer', 'siginterrupt', 'signal']>>>

即通过建立一个回调函数来接收信号,这个回调函数称为信号处理函数(signal hanlder),它会在信号出现时调用。

信号处理函数包括信号编号及被信号中断那一时刻的栈帧。

defhanlder(signum, frame):

something...

signum即信号编号( 数字),例如:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32bit (Intel)] on win32

Type"copyright", "credits" or "license()" formore information.>>> importsignal>>>signal.SIGINT2

>>>

frame为被信号中断那一时刻的栈帧。

==================================================================

接收信号:signal.signal(sig,action)

官方文档:signal.signal(signalnum, handler)Set the handler for signal signalnum to the function handler. handler can be a callable Python object taking two arguments (see below), or one of the special values signal.SIG_IGN or signal.SIG_DFL. The previous signal handler will be returned (see the description of getsignal() above). (See the Unix man page signal(2).)

When threads are enabled, this function can only be called from the main thread; attempting to call it from other threads will cause a ValueError exception to be raised.

The handler is called with two arguments: the signal number and the current stack frame (None or a frame object; for a description of frame objects, see the description in the type hierarchy or see the attribute descriptions in the inspect module).

On Windows, signal() can only be called with SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM. A ValueError will be raised in any other case.

importsignalimportosimporttimedefreceive_signal(signum, stack):print 'Received:', signum#注册信号处理程序

signal.signal(signal.SIGUSR1, receive_signal)

signal.signal(signal.SIGUSR2, receive_signal)#打印这个进程的PID方便使用kill传递信号

print 'My PID is:', os.getpid()

# 等待信号,有信号发生时则调用信号处理程序whileTrue:print 'Waiting...'time.sleep(3)

SIGUSR1和SIGUSR2是留给用户使用的信号。windows下无这两个信号。

这个脚本会无限循环,每次暂停3秒钟。有信号到来时,sleep()调用被中断,信号处理程序receive_signal被调用.信号处理程序返回时,循环继续。

==================================================================

发送信号:os.kill(pid, sig)

>>> importos>>>help(os.kill)

Help on built-in function kill inmodule nt:

kill(...)

kill(pid, sig)

Kill a process with a signal.>>>

pid为进程号, sig为信号

importosimportsignalimporttimedefsignal_usr1(signum, frame):"Callback invoked when a signal is received"pid=os.getpid()print 'Received USR1 in process %s' %pidprint 'Forking...'child_pid=os.fork()ifchild_pid:print 'PARENT: Pausing before sending signal...'time.sleep(1)print 'PARENT: Signaling %s' %child_pid

os.kill(child_pid, signal.SIGUSR1)else:print 'CHILD: Setting up a signal handler'signal.signal(signal.SIGUSR1, signal_usr1)print 'CHILD: Pausing to wait for signal'time.sleep(5)

父进程使用kill()和signal模块向子进程发送信号。在父进程中,使用kill()发送一个USR1信号之前会暂停很短一段时间,这个短暂的暂停使子进程有时间建立信号处理程序。

=================================================================

signal.pause()

官方文档:

signal.pause()Cause the process to sleep until a signal is received; the appropriate handler will then be called. Returns nothing. Not on Windows. (See the Unix man page signal(2).)

等待直到接收一个信号

importsignalimportosimporttimedefdo_exit(sig, stack):raise SystemExit('Exiting')#将SIGINT的默认处理程序替换为SIG_IGN

signal.signal(signal.SIGINT, signal.SIG_IGN)

signal.signal(signal.SIGUSR1, do_exit)print 'My PID:', os.getpid()

signal.pause()

正常情况下,SIGINT会产生一个KeyboardInterrupt,这个例子将忽略SIGINT,并在发现SIGUSR1时产生一个SystemExit。

=================================================================

signal.alarm(time)

官方文档:signal.alarm(time)If time is non-zero, this function requests that a SIGALRM signal be sent to the process in time seconds. Any previously scheduled alarm is canceled (only one alarm can be scheduled at any time). The returned value is then the number of seconds before any previously set alarm was to have been delivered. If time is zero, no alarm is scheduled, and any scheduled alarm is canceled. If the return value is zero, no alarm is currently scheduled. (See the Unix man page alarm(2).) Availability: Unix.

如果time是非0,这个函数则响应一个SIGALRM信号并在time秒后发送到该进程。

importsignalimporttimedefreceived_alarm(signum, stack):print 'Alarm:', time.ctime()#Call receive_alarm in seconds

signal.signal(signal.SIGALRM, received_alarm)

signal.alarm(2)print 'Before:', time.ctime()

time.sleep(4)print 'After:', time.ctime()

================================================================

python小黄人程序_python signal信号相关推荐

  1. Python小黄人绘制

    Python小黄人绘制 使用python turtle库绘制小黄人 ​ 附上各坐标点的坐标图 完整代码: import turtle as t # 初始化 t.setup(800,800) t.pen ...

  2. 用python turtle画小黄人源码_Python turtle模块小黄人程序

    import turtle t = turtle.Turtle() wn = turtle.Screen() turtle.colormode(255) t.hideturtle() t.speed( ...

  3. 用python画小黄人代码-Python turtle模块小黄人程序

    import turtle t = turtle.Turtle() wn = turtle.Screen() turtle.colormode(255) t.hideturtle() t.speed( ...

  4. 用python画小黄人-Python turtle模块小黄人程序

    import turtle t = turtle.Turtle() wn = turtle.Screen() turtle.colormode(255) t.hideturtle() t.speed( ...

  5. 用python画小黄人步骤图-Python turtle模块小黄人程序

    import turtle t = turtle.Turtle() wn = turtle.Screen() turtle.colormode(255) t.hideturtle() t.speed( ...

  6. python画画用哪库好_学Python画画:应用Turtle库画一个蠢萌的小黄人

    t.pensize(4) t.speed(10) 设置画笔的大小.画图的速度,可以改变数值来提升画笔的速度. python学习关注我们企鹅qun: 8393 83765 各类入门学习资料免费分享哦! ...

  7. 用python画小黄人-怎么用python画小黄人

    怎么用python画小黄人? 前言: 还记得小黄人哪只蠢萌蠢萌的单眼小黄人?就是喜欢做什么事都喜欢逞能的那只,下面用Python来实现一下,正在逃跑的小黄人. 一.导入Turtle库 import t ...

  8. 微信小程序相关三、css写小黄人

    小程序上课第三天,因为今天院里有活动,所以没去上课,第四天上午又因为要召开入党转正大会,又耽误了一上午,下午去上课,要了资料.这两天讲了一些零零碎碎的东西,做的实例有上面这个小黄人 都是用的css,基 ...

  9. 如何用python画小黄人_怎么用python画小黄人

    怎么用python画小黄人?TB1免费资源网 前言:TB1免费资源网 还记得小黄人哪只蠢萌蠢萌的单眼小黄人?就是喜欢做什么事都喜欢逞能的那只,下面用Python来实现一下,正在逃跑的小黄人.TB1免费 ...

最新文章

  1. R语言使用ggpubr包的ggarrange函数组合多张结论图:使用ggpubr包将多个可视化结论嵌套起来输出(ggarrange组合ggarrange组合后的图像)
  2. python sanic openapi_2020年了,python的web framework sanic 可以考虑生产环境部署了么?...
  3. 傅里叶分析(matlab)
  4. gitee最多上传多大文件_H5移动端图片压缩上传,基于Jquery的前端,实现拍照上传,选择相册
  5. RTMP协议发送H.264编码及AAC编码的音视频,实现摄像头直播
  6. html input 字体颜色_HTML常用标签汇总
  7. 权重衰减(基于线性回归)
  8. win10绿联usb转串口_绿联usb转串口驱动
  9. 17种最重要的项目管理方法
  10. K3 LEDE踩坑专题
  11. Geometric Transformation(几何变换)
  12. 图片浏览器java程序_图片浏览器用java实现
  13. 虚拟主播是什么,有什么技术原理?- 沉睡者IT
  14. 罗斯蒙特248温度变送器248HANANONS
  15. 我的前端学习之路-----HTML+css(一)
  16. miix5 u盘安装linux,联想Miix510 U盘装系统xp教程
  17. 如何在 Windows 中备份与恢复树莓派 SD 卡
  18. 多媒体课件利用计算机开发工具将,计算机教学中多媒体课件的设计与制作
  19. python一点基础都没有的怎么办-有没有简单一点的 Python 小例子或小项目?
  20. Latex最后的参考文献作者用et.al显示

热门文章

  1. mysqld 进程非常多_MySQL binlog后面的编号最大是多大?
  2. 【数据结构总结】第一章:数据结构基本概念
  3. android如何监听按钮,Android – 两个onClick监听器和一个按钮
  4. Java黑皮书课后题第9章:*9.4(使用Random类)编写一个程序,创建一个种子为1000的Random对象,然后使用nextInt(100)方法显示0到100之间的前50个随机整数
  5. Java黑皮书课后题第4章:*4.9(给出字符的Unicode码)编写程序,得到一个字符的输入,然后显示其Unicode值
  6. 【UOJ】67 新年的毒瘤 【BZOJ】1123 BLO
  7. iOS Xcode全面剖析
  8. Node.js缓冲模块Buffer
  9. UESTC 1851 Kings on a Chessboard
  10. 关于@@IDENTITY、SCOPE_IDENTITY ()、IDENT_CURRENT ('tableName')