python 性能分析

Py-Spy 是一个用于 Python 的性能分析工具,它使用堆栈跟踪来收集和显示 Python 代码的运行时间。

1
2
pip install py-spy
sudo py-spy top --pid 24816

py-spy dump –pid 24816

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Thread 0x7FF8547494C0 (active): "MainThread"
main (data_main.py:36)
<module> (data_main.py:40)
execfile (_pydev_imps/_pydev_execfile.py:18)
_exec (pydevd.py:1570)
run (pydevd.py:1563)
main (pydevd.py:2252)
<module> (pydevd.py:2270)
Thread 0x700005C81000 (idle): "pydevd.Writer"
wait (threading.py:359)
get (queue.py:180)
_on_run (_pydevd_bundle/pydevd_comm.py:367)
run (_pydevd_bundle/pydevd_comm.py:219)
_bootstrap_inner (threading.py:1075)
_bootstrap (threading.py:1032)
Thread 0x700006C84000 (active): "pydevd.Reader"
_on_run (_pydevd_bundle/pydevd_comm.py:291)
run (_pydevd_bundle/pydevd_comm.py:219)
_bootstrap_inner (threading.py:1075)
_bootstrap (threading.py:1032)
Thread 0x700007C87000 (idle): "pydevd.CommandThread"
wait (threading.py:359)
wait (threading.py:655)
_on_run (pydevd.py:159)
run (_pydevd_bundle/pydevd_comm.py:219)
_bootstrap_inner (threading.py:1075)
_bootstrap (threading.py:1032)
Thread 0x700008C8A000 (idle): "Thread-5 (_run_async_loop)"
select (selectors.py:566)
_run_once (pydevd_asyncio/pydevd_nest_asyncio.py:263)
run_until_complete (pydevd_asyncio/pydevd_nest_asyncio.py:234)
_run_async_loop (data/data_collector.py:165)
run (threading.py:1012)
_bootstrap_inner (threading.py:1075)
_bootstrap (threading.py:1032)

aiomonitor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def _run_async_loop(self, poll_interval):
"""
在独立线程中运行 asyncio 事件循环
"""
try:
loop = asyncio.new_event_loop() # 创建新的事件循环
asyncio.set_event_loop(loop) # 设置为当前线程的事件循环
with aiomonitor.start_monitor(loop):
loop.run_until_complete(self._start_async_polling(poll_interval))

logger.info("[INFO] Async loop stopped.")
except Exception as e:
logger.error(f"[ERROR] Async loop encountered an error: {e}")
logger.info("[INFO] Restarting async loop...")

运行 进行交互

1
python -m aiomonitor.cli
Author

Gavin

Posted on

2025-01-14

Updated on

2025-01-14

Licensed under

Comments