導航:首頁 > 編程語言 > python捕捉子方法的異常

python捕捉子方法的異常

發布時間:2023-10-29 18:47:20

python中主線程怎樣捕獲子線程的異常

最近因為別的需求,寫了一個模塊,似乎在這里能用得上:

https://github.com/SakuraSa/ChatProcess


其中的 example.py :

#!/usr/bin/envpython
#coding=utf-8

"""
example
"""

__author__='Rnd495'

fromtimeimportsleep
fromChatProcessimportChatroom


classEcho(Chatroom):
"""
Echo
"""
defresponse(self,data):
ifdata.startswith('sleep'):
sec=float(data[6:])
sleep(sec)
return'wakeupafter%dms'%(sec*1000)
elifdata:
returndata
else:
self.stop()
return'goodbye'


if__name__=='__main__':
,ProcessError

print'process01:'
e=Echo.create_process(lifetime=1).start()
printe.chat('Helloworld!'),e.remain
printe.chat('sleep:0.1'),e.remain
printe.chat(''),e.remain

print''
print'process02:'
e=Echo.create_process(lifetime=1).start()
try:
printe.chat('Helloworld!'),e.remain
printe.chat('sleep:1.0'),e.remain
printe.chat(''),e.remain
exceptTimeoutError,error:
print'error:',error

print''
print'process03:'
e=Echo.create_process(lifetime=1).start()
try:
printe.chat('Helloworld!'),e.remain
printe.chat('sleep:notanum'),e.remain
printe.chat(''),e.remain
exceptProcessError,error:
print'error:',error


運行結果為:

process01:
Helloworld!0.773000001907
wakeupafter100ms0.549000024796
goodbye0.547000169754

process02:
Helloworld!0.868000030518
error:TimeoutError

process03:
Helloworld!0.868000030518
error:('Erroroccurredonloop',ValueError('couldnotconvertstringtofloat:notanum',))


在其中的 process01 中,主進程捕獲了 超時

在其中的 process02 中,主進程捕獲了 子進程的錯誤


不知道你能不能用得上

閱讀全文

與python捕捉子方法的異常相關的資料

熱點內容
appstore中的錢怎麼退 瀏覽:495
單片機程序下載後如何運行 瀏覽:475
剛買的阿里雲伺服器怎樣搭建網站 瀏覽:637
公園設計pdf 瀏覽:684
緩解壓力最好的辦法美國 瀏覽:387
前後端系統數據加密解密 瀏覽:194
中國移動營業app怎麼看套餐 瀏覽:205
javastatic數組 瀏覽:950
需要會員管理源碼 瀏覽:415
手機app如何解除加密 瀏覽:167
用雲伺服器還得買個瘦主機 瀏覽:728
如何查看辦公電腦伺服器地址 瀏覽:368
海星雲的伺服器是什麼系統 瀏覽:411
抖音小籠包解壓神器 瀏覽:558
手機下載的源碼在哪裡儲存 瀏覽:846
pdf看三維 瀏覽:406
九宮演算法干什麼用的 瀏覽:907
phpjava性能比較 瀏覽:886
2016會計中級pdf 瀏覽:181
農村信用社app怎麼刪除明細 瀏覽:818