Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # encoding=utf-8
  3. """
  4. filename: debug_test.py
  5. description: debug a running process
  6. License: GPL V2
  7. Author: 天使de眼睛
  8.  
  9. 除特别声明,所有代码均是 python3.6 在 iOS 环境下编写测试。
  10. """
  11. #!/usr/bin/env python
  12. # -*- encoding: utf-8 -*-
  13. import code
  14. import traceback
  15. import signal
  16. import time
  17. import os
  18. import random
  19.  
  20. def debug(sig, frame):
  21. df={'_frame':frame} # 把 frame 放在一个字典下
  22. df.update(frame.f_globals) # 把globals直接放入字典
  23. df.update(frame.f_locals) # locals 放入字典
  24.  
  25. interactive = code.InteractiveConsole(df)
  26. message = "调试吧:\nTraceback:\n"
  27. message += ''.join(traceback.format_stack(frame))
  28. interactive.interact(message)
  29.  
  30. def trigger():
  31. signal.signal(signal.SIGTERM, debug)
  32.  
  33. def test():
  34. while True:
  35. int_a = random.randint(1, 1000)
  36. int_b = random.randint(1, 1000)
  37. result = int_a + int_b
  38. time.sleep(1)
  39.  
  40. def main():
  41. print("start main")
  42. trigger()
  43. test()
  44.  
  45. def x():
  46. print("enter thread")
  47. time.sleep(0.2)
  48. for i in range(1,6):
  49. print('sleep :', i)
  50. time.sleep(1)
  51. print("try to send kill signal")
  52. print()
  53. time.sleep(0.2)
  54. os.kill(os.getpid(), signal.SIGTERM)
  55. print("killed")
  56.  
  57. if __name__ == '__main__':
  58. print("pid", os.getpid())
  59. import threading
  60. t = threading.Thread(target=x)
  61. t.start()
  62. time.sleep(0.1)
  63. main()
  64. t.join()
Add Comment
Please, Sign In to add comment