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

熱點內容
資料庫查詢系統源碼 瀏覽:617
php5314 瀏覽:358
完美國際安裝到哪個文件夾 瀏覽:669
什麼app可以掃一掃做題 瀏覽:540
程序員編碼論壇 瀏覽:924
淘點是什麼app 瀏覽:660
中國高等植物pdf 瀏覽:454
51單片機時間 瀏覽:182
後台如何獲取伺服器ip 瀏覽:267
單片機流水燈程序c語言 瀏覽:236
程序員第二職業掙錢 瀏覽:240
運行里怎麼輸入伺服器路徑 瀏覽:843
pythonstepwise 瀏覽:513
劉一男詞彙速記指南pdf 瀏覽:66
php認證級別 瀏覽:371
方舟編譯啥時候推送 瀏覽:1013
php手機驗證碼生成 瀏覽:677
哲學思維pdf 瀏覽:17
凌達壓縮機有限公司招聘 瀏覽:536
weblogic命令部署 瀏覽:39