improving speed of Python module import

前端 未结 4 1556
[愿得一人]
[愿得一人] 2020-12-07 16:12

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

4条回答
  •  Happy的楠姐
    2020-12-07 16:58

    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.

    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

    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()
    

    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)
    

    you just run the server:

    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
    

    then the server updates the plot.

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

提交回复
热议问题