提高Python模块导入的速度 [英] improving speed of Python module import

查看:2290
本文介绍了提高Python模块导入的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前曾询问过如何加速导入Python模块的问题(超速pythonimport加载器 Python - 加速进口? )但没有具体的例子,并没有产生公认的解决方案。因此,我将在这里再次讨论这个问题,但这次是一个具体的例子。

The question of how to speed up importing of Python modules has been asked previously (Speeding up the python "import" loader and Python -- Speed Up Imports?) but without specific examples and has not yielded accepted solutions. I will therefore take up the issue again here, but this time with a specific example.

我有一个Python脚本,可以从磁盘加载三维图像堆栈,平滑它并将其显示为电影。当我想快速查看我的数据时,我从系统命令提示符调用此脚本。我可以用700毫秒来平滑数据,因为这与MATLAB相当。但是,导入模块需要额外650 ms。因此,从用户的角度来看,Python代码以一半的速度运行。

I have a Python script that loads a 3-D image stack from disk, smooths it, and displays it as a movie. I call this script from the system command prompt when I want to quickly view my data. I'm OK with the 700 ms it takes to smooth the data as this is comparable to MATLAB. However, it takes an additional 650 ms to import the modules. So from the user's perspective the Python code runs at half the speed.

这是我正在导入的一系列模块:

This is the series of modules I'm importing:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import scipy.ndimage
import scipy.signal
import sys
import os

当然,不是全部模块导入速度同样慢。主要罪魁祸首是:

Of course, not all modules are equally slow to import. The chief culprits are:

matplotlib.pyplot   [300ms]
numpy               [110ms]
scipy.signal        [200ms]

我已尝试使用 >,但这不是更快。由于Matplotlib是主要的罪魁祸首,它因缓慢的屏幕更新而闻名,我寻找替代方案。一个是PyQtGraph,但导入需要550毫秒。

I have experimented with using from, but this isn't any faster. Since Matplotlib is the main culprit and it's got a reputation for slow screen updates, I looked for alternatives. One is PyQtGraph, but that takes 550 ms to import.

我知道一个明显的解决方案,即从交互式Python会话而不是系统命令提示符调用我的函数。这很好但是它太像MATLAB了,我更喜欢从系统提示中获得我的功能的优雅。

I am aware of one obvious solution, which is to call my function from an interactive Python session rather than the system command prompt. This is fine but it's too MATLAB-like, I'd prefer the elegance of having my function available from the system prompt.

我是Python的新手,我不知道如何继续这一点。由于我是新手,我很欣赏如何实施建议解决方案的链接。理想情况下,我正在寻找一个简单的解决方案(不是我们所有人!)因为代码需要在多台Mac和Linux机器之间移植。

I'm new to Python and I'm not sure how to proceed at this point. Since I'm new, I'd appreciate links on how to implement proposed solutions. Ideally, I'm looking for a simple solution (aren't we all!) because the code needs to be portable between multiple Mac and Linux machines.

推荐答案

你可以构建一个简单的服务器/客户端,服务器不断运行并更新绘图,客户端只是在沟通下一个要处理的文件。

you could build a simple server/client, the server running continuously making and updating the plot, and the client just communicating the next file to process.

我根据 socket 模块中的基本示例编写了一个简单的服务器/客户端示例docs: http://docs.python.org/2/library/socket.html #example

I wrote a simple server/client example based on the basic example from the socket module docs: http://docs.python.org/2/library/socket.html#example

这里是server.py:

here is server.py:

# expensive imports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import scipy.ndimage
import scipy.signal
import sys
import os

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while 1:
    conn, addr = s.accept()
    print 'Connected by', addr
    data = conn.recv(1024)
    if not data: break
    conn.sendall("PLOTTING:" + data)
    # update plot
    conn.close()

和client.py:

and client.py:

# Echo client program
import socket
import sys

HOST = ''    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(sys.argv[1])
data = s.recv(1024)
s.close()
print 'Received', repr(data)

您只需运行服务器:

python server.py

执行导入,然后客户端通过套接字要绘制的新文件的文件名:

which does the imports, then the client just sends via the socket the filename of the new file to plot:

python client.py mytextfile.txt

然后服务器更新图。

在我的机器上运行你的导入需要0.6秒,同时运行 client.py 0.03秒。

On my machine running your imports take 0.6 seconds, while running client.py 0.03 seconds.

这篇关于提高Python模块导入的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆