導航:首頁 > 編程語言 > 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捕捉子方法的異常相關的資料

熱點內容
相片如何用電解壓 瀏覽:905
碩士程序員去學校當老師 瀏覽:120
pythonstr提取到字典 瀏覽:818
程序員那麼可愛有人看上陸漓了 瀏覽:876
php正則提取圖片 瀏覽:103
pythonlinuxdjango 瀏覽:562
php中文返回亂碼 瀏覽:89
宿舍裝的電信怎麼加密 瀏覽:745
為什麼壓縮文件解壓後變少了 瀏覽:426
現在安卓充電器普遍是什麼型號 瀏覽:714
9日均線36均線主圖指標源碼 瀏覽:349
程序員阿里文化完整版 瀏覽:98
早間新聞在哪個app上面可以看 瀏覽:954
工作啦app注冊的信息怎麼刪去 瀏覽:378
滾動轉子式製冷壓縮機 瀏覽:873
美國編程用什麼軟體 瀏覽:571
圖片加密防盜用 瀏覽:616
dbscan演算法python源碼 瀏覽:849
固態硬碟文件夾刪不掉 瀏覽:717
陽泉工會app怎麼培訓報名 瀏覽:669