欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

探尋python多線程ctrl+c退出問題解決方案

系統 1837 0

場景:

經常會遇到下述問題:很多io busy的應用采取多線程的方式來解決,但這時候會發現python命令行不響應ctrl-c 了,而對應的java代碼則沒有問題:

復制代碼 代碼如下:

public class Test {?
??? public static void main(String[] args) throws Exception {?
?
??????? new Thread(new Runnable() {?
?
??????????? public void run() {?
??????????????? long start = System.currentTimeMillis();?
??????????????? while (true) {?
??????????????????? try {?
??????????????????????? Thread.sleep(1000);?
??????????????????? } catch (Exception e) {?
??????????????????? }?
??????????????????? System.out.println(System.currentTimeMillis());?
??????????????????? if (System.currentTimeMillis() - start > 1000 * 100) break;?
??????????????? }?
??????????? }?
??????? }).start();?
?
??? }?
}?
java Test

ctrl-c則會結束程序

而對應的python代碼:

復制代碼 代碼如下:

# -*- coding: utf-8 -*-?
import time?
import threading?
start=time.time()?
def foreverLoop():?
??? start=time.time()?
??? while 1:?
??????? time.sleep(1)?
??????? print time.time()?
??????? if time.time()-start>100:?
??????????? break?
??????????????
thread_=threading.Thread(target=foreverLoop)?
#thread_.setDaemon(True)?
thread_.start()?

python p.py

后ctrl-c則完全不起作用了。

不成熟的分析:

首先單單設置 daemon 為 true 肯定不行,就不解釋了。當daemon為 false 時,導入python線程庫后實際上,threading會在主線程執行完畢后,檢查是否有不是 daemon 的線程,有的化就wait,等待線程結束了,在主線程等待期間,所有發送到主線程的信號也會被阻測,可以在上述代碼加入signal模塊驗證一下:

復制代碼 代碼如下:

def sigint_handler(signum,frame):???
??? print "main-thread exit"?
??? sys.exit()???
signal.signal(signal.SIGINT,sigint_handler)?

在100秒內按下ctrl-c沒有反應,只有當子線程結束后才會出現打印 "main-thread exit",可見 ctrl-c被阻測了

threading 中在主線程結束時進行的操作:

復制代碼 代碼如下:

_shutdown = _MainThread()._exitfunc?
def _exitfunc(self):?
??????? self._Thread__stop()?
??????? t = _pickSomeNonDaemonThread()?
??????? if t:?
??????????? if __debug__:?
??????????????? self._note("%s: waiting for other threads", self)?
??????? while t:?
??????????? t.join()?
??????????? t = _pickSomeNonDaemonThread()?
??????? if __debug__:?
??????????? self._note("%s: exiting", self)?
??????? self._Thread__delete()?
?

?對所有的非daemon線程進行join等待,其中join中可自行察看源碼,又調用了wait,同上文分析 ,主線程等待到了一把鎖上。

不成熟的解決:

只能把線程設成daemon才能讓主線程不等待,能夠接受ctrl-c信號,但是又不能讓子線程立即結束,那么只能采用傳統的輪詢方法了,采用sleep間歇省點cpu吧:
?

復制代碼 代碼如下:

# -*- coding: utf-8 -*-?
import time,signal,traceback?
import sys?
import threading?
start=time.time()?
def foreverLoop():?
??? start=time.time()?
??? while 1:?
??????? time.sleep(1)?
??????? print time.time()?
??????? if time.time()-start>5:?
??????????? break?
?????????????
thread_=threading.Thread(target=foreverLoop)?
thread_.setDaemon(True)?
thread_.start()?
?
#主線程wait住了,不能接受信號了?
#thread_.join()?
?
def _exitCheckfunc():?
??? print "ok"?
??? try:?
??????? while 1:?
??????????? alive=False?
??????????? if thread_.isAlive():?
??????????????? alive=True?
??????????? if not alive:?
??????????????? break?
??????????? time.sleep(1)???
??? #為了使得統計時間能夠運行,要捕捉? KeyboardInterrupt :ctrl-c???????
??? except KeyboardInterrupt, e:?
??????? traceback.print_exc()?
??? print "consume time :",time.time()-start?
?????????
threading._shutdown=_exitCheckfunc?

?? 缺點:輪詢總會浪費點cpu資源,以及battery.

有更好的解決方案敬請提出。

ps1: 進程監控解決方案 :

用另外一個進程來接受信號后殺掉執行任務進程,牛

復制代碼 代碼如下:

# -*- coding: utf-8 -*-?
import time,signal,traceback,os?
import sys?
import threading?
start=time.time()?
def foreverLoop():?
??? start=time.time()?
??? while 1:?
??????? time.sleep(1)?
??????? print time.time()?
??????? if time.time()-start>5:?
??????????? break?
?
class Watcher:?
??? """this class solves two problems with multithreaded
??? programs in Python, (1) a signal might be delivered
??? to any thread (which is just a malfeature) and (2) if
??? the thread that gets the signal is waiting, the signal
??? is ignored (which is a bug).
?
??? The watcher is a concurrent process (not thread) that
??? waits for a signal and the process that contains the
??? threads.? See Appendix A of The Little Book of Semaphores.
??? http://greenteapress.com/semaphores/
?
??? I have only tested this on Linux.? I would expect it to
??? work on the Macintosh and not work on Windows.
??? """?
?
??? def __init__(self):?
??????? """ Creates a child thread, which returns.? The parent
??????????? thread waits for a KeyboardInterrupt and then kills
??????????? the child thread.
??????? """?
??????? self.child = os.fork()?
??????? if self.child == 0:?
??????????? return?
??????? else:?
??????????? self.watch()?
?
??? def watch(self):?
??????? try:?
??????????? os.wait()?
??????? except KeyboardInterrupt:?
??????????? # I put the capital B in KeyBoardInterrupt so I can?
??????????? # tell when the Watcher gets the SIGINT?
??????????? print 'KeyBoardInterrupt'?
??????????? self.kill()?
??????? sys.exit()?
?
??? def kill(self):?
??????? try:?
??????????? os.kill(self.child, signal.SIGKILL)?
??????? except OSError: pass?
?
Watcher()?????????????
thread_=threading.Thread(target=foreverLoop)?
thread_.start()?

?注意 watch()一定要放在線程創建前,原因未知。。。。,否則立刻就結束


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线观看黄色小视频 | 韩国理论午夜 | 欧美一区二区在线观看视频 | 精品毛片| 妇女毛片 | 黄色免费在线观看网址 | 色诱成人免费观看视频 | 日韩一区二区av | 精品久久久久久一区二区 | 成人在线精品 | 亚洲欧美成人综合在线 | 91视频在线网站 | japanese xxxxhd| 欧日韩在线视频 | 人人干人人看 | 嫩草网站| 日韩在线观看网站 | 日本成熟视频tube~be | 久久久久久久一区二区三区 | 久久久亚洲伊人色综合网站 | 国产成人免费永久播放视频平台 | 免费一区 | 99青草青草久热精品视频 | 91在线最新 | 澳门永久av免费网站 | 欧洲一级鲁丝片免费 | 久久成人久久爱 | 国产精品久久久久久久久久红粉 | 一级欧美视频 | 欧美日韩一区二区视频在线观看 | 天天躁天天碰天天看 | 亚洲欧美一区二区三区综合 | 天天干天天操天天爽 | 免费国产成人午夜在线观看 | 日韩在线精品视频 | 日日爱夜夜操 | 亚洲a级在线观看 | 国产欧美性综合视频性刺激 | 成人欧美一区在线视频在线观看 | 国产成人精品久久亚洲高清不卡 | 福利视频在线免费观看 |