I'm seeing an issue where the execution of my python script can sometimes take longer to import libraries. This happens if I have not executed the script recently or if I run the script from a different server. After the first delay, the import times become much faster. I was wondering what is causing this slow import and if there is any way to prevent it?
import time
s_time = time.time()
import sys,re,os,logging,signal
from argparse import ArgumentParser
print('Internal Import Time: {}'.format(time.time() - s_time))
s_time = time.time()
from backtrace import Backtrace,BacktraceSet
from report import Report
from core import Core
from burtapi import BurtAPI
print('External Import Time: {}'.format(time.time() - s_time))
In this example backtrace,report,core, and burtapi are libraries I created.
[name@server1 tool]$ ./tool --python
Internal Import Time: 2.8281359672546387
External Import Time: 13.053943157196045
Enter/Paste your content. Ctrl-D to save it.
^CYou pressed Ctrl+C!
[name@server1 tool]$ ./tool --python
Internal Import Time: 0.12279081344604492
External Import Time: 0.6948020458221436
Enter/Paste your content. Ctrl-D to save it.
^CYou pressed Ctrl+C!
ssh to different server (with same storage mount)
[name@server2 tool]$ ./tool --python
Internal Import Time: 3.0217390060424805
External Import Time: 13.151482105255127
Enter/Paste your content. Ctrl-D to save it.
^CYou pressed Ctrl+C!
[name@server2 tool]$
./tool is a bash script that calls python3 /path/to/script.py