Skip to main content
 首页 » 编程设计

Python IDLE 在非常大的程序输入上变慢

2024年11月01日10artech

为什么 python idle 在处理非常大的输入时变得如此缓慢,而 python 命令行却没有?

例如,如果我在 python IDLE 中运行“aman”*10000000,它变得没有响应,但在 python cmd 行上,它很快。

请您参考如下方法:

我不得不研究一下。当我在我的机器上调用 idle 时,我看到另一个使用 idlelib

的 python 进程
~$ ps -eaf | grep -in idle 
234:1000     13122     1  5 16:44 ?        00:00:01 /usr/bin/python2.7 /usr/bin/idle-python2.7 
235:1000     13124 13122  3 16:44 ?        00:00:01 /usr/bin/python2.7 -c __import__('idlelib.run').run.main(True) 60839 
239:1000     13146 12061  0 16:44 pts/0    00:00:00 grep --color=auto -in idle 
~$  

最后一个参数(60839)让我思考。所以我四处寻找 idlelib 并在此处获得了实现 https://github.com/pypy/pypy/blob/master/lib-python/2.7/idlelib/run.py#L49那里的评论说

Start the Python execution server in a subprocess 
 
    In the Python subprocess, RPCServer is instantiated with handlerclass 
    MyHandler, which inherits register/unregister methods from RPCHandler via 
    the mix-in class SocketIO. 

现在,事情对我来说很清楚了。 IDLE 通过 TCP 连接向 python 解释器发送命令。尽管如此,我还是不相信。然后我阅读了完整的Help->About IDLE->README。它说

IDLE executes Python code in a separate process, which is restarted for each Run (F5) initiated from an editor window. The environment can also be restarted from the Shell window without restarting IDLE.

结论

当我们有这样的依赖关系时(IDLE 取决于通过套接字的响应),您所经历的延迟完全没问题。