99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Python3運(yùn)行時(shí)查看線程信息的方法詳解

發(fā)布時(shí)間:2018-04-18 15:15  回復(fù):0  查看:2860   最后回復(fù):2018-04-18 15:15  
本文和大家分享的主要是python3 運(yùn)行時(shí)查看線程信息相關(guān)內(nèi)容,在 python3 中引入faulthandler  模塊,就可以簡(jiǎn)化我們很多工作,具體操作,下文將會(huì)為大家詳細(xì)介紹,希望通過本文分享對(duì)大家 學(xué)習(xí)python3有所幫助。
  先準(zhǔn)備一個(gè)小程序,就是周期性的往命令行終端輸出一下時(shí)間,如下:
  #!/usr/bin/env python# -*- coding: utf-8 -*- import os import sys import threading import traceback import signal import tempfile from datetime  import datetime import time
   def  test():
   while  True:
  print(datetime.now())
  time.sleep(2)
   if __name__ == "__main__":
   try:
  threading.Thread(target=test).start()
   while  True:
  time.sleep(60)
   except KeyboardInterrupt:
  sys.exit(1)
  要啟用 dump thread  的功能,可以通過三種方式來(lái)打開:
  1.  使用  PYTHONFAULTHANDLER=true  環(huán)境變量
  2.  使用  python3 -q -X faulthandler  來(lái)起用
  3.  代碼方式來(lái)啟用
  這里我們通過設(shè)置環(huán)境變量 “PYTHONFAULTHANDLER=true”  來(lái)啟用,用下面方式運(yùn)行程序:
  $ PYTHONFAULTHANDLER=true ./test.py
  然后在另開一個(gè)終端運(yùn)行下面的命令
  $ kill -SIGABRT ` ps -ef |  grep test. py |  grep -v 'grep' | awk '{print $2}'`
  此時(shí)我們可以在運(yùn)行 test.py  的終端中看到如下線程信息:
  Fatal Python error: Aborted
  Thread 0x00007f8298430700 (most recent  call  first):
   File "./test3.py", line 16  in  test
   File "/usr/lib64/python3.4/threading.py", line 859  in run
   File "/usr/lib64/python3.4/threading.py", line 911  in _bootstrap_inner
   File "/usr/lib64/python3.4/threading.py", line 879  in _bootstrap
   Current  thread 0x00007f82a2fcf740 (most recent  call  first):
   File "./test3.py", line 24  in < module>
  Aborted (core dumped)
  這里我們是通過發(fā)送 SIGABRT  信號(hào)來(lái)觸發(fā)的,其實(shí)也可以使用  SIGSEGV, SIGFPE, SIGABRT, SIGBUS  和  SIGILL  信號(hào)來(lái)出發(fā)。
  運(yùn)行上面的程序,我們發(fā)現(xiàn)一個(gè)問題,一旦dump threads 后,進(jìn)程就退出了,而如果我們不想讓進(jìn)程退出,該怎么辦呢,這就要用到前一篇文章中說提到的復(fù)寫信號(hào)處理函數(shù)了。
  看一下下面的代碼,我們和前一篇博客中一樣都是復(fù)寫 “SIGQUIT”  信號(hào)處理函數(shù),在接受到  SIGQUIT  信號(hào)后,輸出線程信息到  /tmp  目錄下,然后程序繼續(xù)運(yùn)行。
  #!/usr/bin/env /usr/bin/python3.4# -*- coding: utf-8 -*- import os import sys import threading import traceback import signal import tempfile from datetime  import datetime import time import faulthandler
   def  test():
   while  True:
  print(datetime.now())
  time.sleep(2)
   def  signal_handler(signum, frame):
   try:
  file = os.path.join(tempfile.gettempdir(), datetime.now().strftime('%Y%m%d%H%M%S') + ".log")
   with open(file, 'w+')  as f:
  faulthandler.dump_traceback(file=f, all_threads= True)
   except BaseException  as e:
  print(e)
   if __name__ == "__main__":
   try:
  signal.signal(signal.SIGQUIT, signal_handler)
  threading.Thread(target=test).start()
   while  True:
  time.sleep(60)
   except KeyboardInterrupt:
  sys.exit(1)
  運(yùn)行上面的程序,并使用下面的命令來(lái)給進(jìn)程發(fā) SIGQUIT  信號(hào)
  $ kill -SIGQUIT ` ps -ef |  grep test. py |  grep -v 'grep' | awk '{print $2}'`
  然后可以在 /tmp  目錄下找到輸出的線程日志,內(nèi)容如下,同時(shí)我們也可以看到程序會(huì)繼續(xù)執(zhí)行下去而不會(huì)推出。
  Thread 0x00007f13d75d2700 (most recent  call  first):
   File "./test3.py", line 17  in  test
   File "/usr/lib64/python3.4/threading.py", line 859  in run
   File "/usr/lib64/python3.4/threading.py", line 911  in _bootstrap_inner
   File "/usr/lib64/python3.4/threading.py", line 879  in _bootstrap
   Current  thread 0x00007f13e2171740 (most recent  call  first):
   File "./test3.py", line 24  in signal_handler
   File "./test3.py", line 36  in < module>
來(lái)源:網(wǎng)絡(luò)
您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?