避免 python 设置时间

发布于 11-17 00:45 字数 180 浏览 2 评论 0原文

下图显示 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 ??

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澉约2024-11-24 00:45:43

我只是搜索了相同的内容,发现了这个:

http:// blogs.gnome.org/johan/2007/01/18/introducing-python-launcher/

Python-launcher 并没有直接解决问题,但它指出了一个有趣的方向:如果你创建一个小守护进程,您可以通过 shell 联系它来派生一个新实例,您也许可以摆脱启动时间。

例如,获取 python-launcher 和 socat1 并执行以下操作:

PYTHONPATH="../lib.linux-x86_64-2.7/" python python-launcher-daemon &
echo pass > 1
for i in {1..100}; do 
    echo 1 | socat STDIN UNIX-CONNECT:/tmp/python-launcher-daemon.socket & 
done

Todo:将其适应您的程序,删除 GTK 内容。请注意 &最后:关闭套接字连接似乎很慢。

基本技巧是创建一个打开套接字的服务器。然后它从套接字读取所有数据。一旦获得数据,它就会像下面这样分叉:

        pid = os.fork()
        if pid:
            return

        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
        signal.signal(signal.SIGCHLD, signal.SIG_DFL)

        glob = dict(__name__="__main__")
        print 'launching', program
        execfile(program, glob, glob)

        raise SystemExit

对我来说,运行 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:

PYTHONPATH="../lib.linux-x86_64-2.7/" python python-launcher-daemon &
echo pass > 1
for i in {1..100}; do 
    echo 1 | socat STDIN UNIX-CONNECT:/tmp/python-launcher-daemon.socket & 
done

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:

        pid = os.fork()
        if pid:
            return

        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
        signal.signal(signal.SIGCHLD, signal.SIG_DFL)

        glob = dict(__name__="__main__")
        print 'launching', program
        execfile(program, glob, glob)

        raise SystemExit

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

晨曦慕雪2024-11-24 00:45:43

在 Python 脚本中编写“执行此操作 100 次”逻辑。从另一种语言中调用它一次。

Write the "do this several 100 times" logic in your Python script. Call it ONCE from that other language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文