Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # encoding=utf-8
- """
- filename: debug_test.py
- description: debug a running process
- License: GPL V2
- Author: 天使de眼睛
- 除特别声明,所有代码均是 python3.6 在 iOS 环境下编写测试。
- """
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- import code
- import traceback
- import signal
- import time
- import os
- import random
- def debug(sig, frame):
- df={'_frame':frame} # 把 frame 放在一个字典下
- df.update(frame.f_globals) # 把globals直接放入字典
- df.update(frame.f_locals) # locals 放入字典
- interactive = code.InteractiveConsole(df)
- message = "调试吧:\nTraceback:\n"
- message += ''.join(traceback.format_stack(frame))
- interactive.interact(message)
- def trigger():
- signal.signal(signal.SIGTERM, debug)
- def test():
- while True:
- int_a = random.randint(1, 1000)
- int_b = random.randint(1, 1000)
- result = int_a + int_b
- time.sleep(1)
- def main():
- print("start main")
- trigger()
- test()
- def x():
- print("enter thread")
- time.sleep(0.2)
- for i in range(1,6):
- print('sleep :', i)
- time.sleep(1)
- print("try to send kill signal")
- print()
- time.sleep(0.2)
- os.kill(os.getpid(), signal.SIGTERM)
- print("killed")
- if __name__ == '__main__':
- print("pid", os.getpid())
- import threading
- t = threading.Thread(target=x)
- t.start()
- time.sleep(0.1)
- main()
- t.join()
Add Comment
Please, Sign In to add comment