improving speed of Python module import

前端 未结 4 1566
[愿得一人]
[愿得一人] 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条回答
  •  旧时难觅i
    2020-12-07 16:53

    1.35 seconds isn't long, but I suppose if you're used to half that for a "quick check" then perhaps it seems so.

    Andrea suggests a simple client/server setup, but it seems to me that you could just as easily call a very slight modification of your script and keep it's console window open while you work:

    • Call the script, which does the imports then waits for input
    • Minimize the console window, switch to your work, whatever: *Do work*
    • Select the console again
    • Provide the script with some sort of input
    • Receive the results with no import overhead
    • Switch away from the script again while it happily awaits input

    I assume your script is identical every time, ie you don't need to give it image stack location or any particular commands each time (but these are easy to do as well!).

    Example RAAC's_Script.py:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    import scipy.ndimage
    import scipy.signal
    import sys
    import os
    
    print('********* RAAC\'s Script Now Running *********')
    
    while True: # Loops forever
        # Display a message and wait for user to enter text followed by enter key.
        # In this case, we're not expecting any text at all and if there is any it's ignored
        input('Press Enter to test image stack...')
    
        '''
        *
        *
        **RAAC's Code Goes Here** (Make sure it's indented/inside the while loop!)
        *
        *
        '''
    

    To end the script, close the console window or press ctrl+c.

    I've made this as simple as possible, but it would require very little extra to handle things like quitting nicely, doing slightly different things based on input, etc.

提交回复
热议问题