我有一个使用线程的脚本,该脚本在运行一个小时左右后便会冻结,这使得调试非常困难。
最终,我找到了pyrasite
,基本上,等到它冻结后,我就可以“钩”入脚本。在终端中看起来像这样:
$ pyrasite-shell 3437
Pyrasite Shell 2.0
Connected to 'python3 code/MyTestScript.py'
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
(DistantInteractiveConsole)
>>> import sys, traceback
>>> for thread_id, frame in sys._current_frames().items():
... print('Stack for thread {}'.format(thread_id))
... traceback.print_stack(frame)
... print('')
...
Stack for thread 1888482416
File "/usr/lib/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "<string>", line 167, in run
File "/usr/lib/python3.5/code.py", line 228, in interact
more = self.push(line)
File "/usr/lib/python3.5/code.py", line 250, in push
more = self.runsource(source, self.filename)
File "/usr/lib/python3.5/code.py", line 75, in runsource
self.runcode(code)
File "/usr/lib/python3.5/code.py", line 91, in runcode
exec(code, self.locals)
File "<console>", line 3, in <module>
Stack for thread 1898968176
File "/usr/lib/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/code/my_test_lib.py", line 187, in sendData
self.evexit.wait(sleep_time_s)
File "/usr/lib/python3.5/threading.py", line 549, in wait
signaled = self._cond.wait(timeout)
File "/usr/lib/python3.5/threading.py", line 297, in wait
gotit = waiter.acquire(True, timeout)
Stack for thread 1996019264
File "code/MyTestScript.py", line 1355, in <module>
main(sys.argv[1:])
File "code/MyTestScript.py", line 1245, in main
myObject.waitForJoin() # seems to work fine...
File "/home/pi/code/my_test_lib.py", line 251, in waitForJoin
self.myThread.join()
File "/usr/lib/python3.5/threading.py", line 1054, in join
self._wait_for_tstate_lock()
File "/usr/lib/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
Stack for thread 1908950128
File "/usr/lib/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/code/my_testB_lib.py", line 511, in UpdateMyThread
time.sleep(_update_interval_sec)
这看起来不错,但我无法确定的是,包括其所有线程的脚本现在是否已暂停(好像已在
gdb
中设置了断点),还是该脚本一直在后台运行?无论如何,我知道在
gdb
中,我可以刚刚发出thread 1
,然后结束于相应的堆栈帧,然后可以打印局部变量等。但是,在这里,我无法告诉您如何更改堆栈框架,也不能告诉您如何切换线程,以便可以检查变量。
pyrasite
是否可能?如果没有,那么是否还有其他适用于Python3的库,这将使我具有相同的库(即能够冻结具有冻结线程的未插入脚本的能力),同时允许我检查任何/所有线程和堆栈框架中的局部变量值?
最佳答案
从Pyrasite包含的dump_stacks.py
有效内容开始,我创建了以下代码段,该代码段在每次堆栈跟踪之后转储局部变量名称和值。但是,它似乎不能作为Pyrasite负载正常工作-看起来由于某些原因而显示了全局变量吗?我本人是Pyrasite的新手,所以不确定这是Bug还是使用不正确。如果我改为运行pyrasite-shell
并直接将代码粘贴到其中,则它将按预期工作(忽略了来自 shell 线程本身的额外跟踪)。
如果您需要查看其他信息,则可以修改代码并重新粘贴,或者仅在吡硫铁矿 shell 中进行交互浏览。
import sys, traceback
for thread, frame in sys._current_frames().items():
print('Thread 0x%x' % thread)
print('=' * 25)
traceback.print_stack(frame)
print('-' * 25)
for name, value in frame.f_locals.items():
print(f"{name}: {value}")
print()
关于python - 在Python(pyrasite)中检查线程中的局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454702/