避免 python 设置时间
下图显示 python 在用户空间中花费了大量时间。有可能减少这个时间吗?
从某种意义上说,我将运行一个脚本数百次。是否可以启动python,以便需要时间来初始化一次,并且在后续时间不执行它?
This image below says python takes lot of time in user space. Is it possible to reduce this time at all ?
In the sense I will be running a script several 100 times. Is it possible to start python so that it takes time to initialize once and doesn't do it the subsequent time ??
我只是搜索了相同的内容,发现了这个:
http:// blogs.gnome.org/johan/2007/01/18/introducing-python-launcher/
Python-launcher 并没有直接解决问题,但它指出了一个有趣的方向:如果你创建一个小守护进程,您可以通过 shell 联系它来派生一个新实例,您也许可以摆脱启动时间。
例如,获取 python-launcher 和 socat1 并执行以下操作:
Todo:将其适应您的程序,删除 GTK 内容。请注意 &最后:关闭套接字连接似乎很慢。
基本技巧是创建一个打开套接字的服务器。然后它从套接字读取所有数据。一旦获得数据,它就会像下面这样分叉:
对我来说,运行 100 个程序只花了 0.7 秒。
如果您想非常快,您可能必须从分叉切换到仅执行代码而不是分叉。
(这也是我对 emacsclient 所做的事情……我的 emacs 需要大约 30 秒才能启动(由于过度使用我添加的其他库),但 emacsclient -c 几乎立即显示。)
1: http://www.socat.org
I just searched for the same and found this:
http://blogs.gnome.org/johan/2007/01/18/introducing-python-launcher/
Python-launcher does not solve the problem directly, but it points into an interesting direction: If you create a small daemon which you can contact via the shell to fork a new instance, you might be able to get rid of your startup time.
For example get the python-launcher and socat¹ and do the following:
Todo: Adapt it to your program, remove the GTK stuff. Note the & at the end: Closing the socket connection seems to be slow.
The essential trick is to just create a server which opens a socket. Then it reads all the data from the socket. Once it has the data, it forks like the following:
Running 100 programs that way took just 0.7 seconds for me.
You might have to switch from forking to just executing the code instead of forking if you want to be really fast.
(That’s what I also do with emacsclient… My emacs takes ~30s to start (due to excessive use of additional libraries I added), but emacsclient -c shows up almost instantly.)
¹: http://www.socat.org