1 Star 0 Fork 0

dzc/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
thread_signal.py 788 Bytes
一键复制 编辑 原始数据 按行查看 历史
cclauss 提交于 2018-11-28 16:55 +08:00 . Use print() function in both Python 2 and Python 3
from __future__ import print_function
import threading
from time import sleep
import signal
class producer(threading.Thread):
def __init__(self, event):
threading.Thread.__init__(self)
self.event = event
def run(self):
while self.event.is_set():
print("sub thread")
sleep(2)
else:
print("sub thread end")
exit()
def handler_thread(event):
print("main thread end")
event.clear()
def handler(signum, frame):
handler_thread(frame.f_globals['event'])
signal.signal(signal.SIGINT, handler)
print("main thread")
event = threading.Event()
event.set()
p = producer(event)
p.start()
p.join()
sleep(100) # 一定要使主线程处于活动状态,否则信号处理对子线程不起作用
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdredboy/Python-1.git
git@gitee.com:sdredboy/Python-1.git
sdredboy
Python-1
Python-1
master

搜索帮助