feat: optimize slow imports loading based on version and executable

This commit is contained in:
Paul Gauthier (aider) 2024-09-08 09:07:19 -07:00
parent ed7a9c6562
commit f54b6860db

View file

@ -705,24 +705,52 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
def launch_slow_imports_thread():
thread = threading.Thread(target=load_slow_imports)
thread = threading.Thread(target=check_and_load_imports)
thread.daemon = True
thread.start()
def check_and_load_imports():
import json
from pathlib import Path
installs_file = Path.home() / ".aider" / "installs.json"
key = (__version__, sys.executable)
try:
if installs_file.exists():
with open(installs_file, "r") as f:
installs = json.load(f)
else:
installs = {}
if str(key) not in installs:
load_slow_imports()
installs[str(key)] = True
installs_file.parent.mkdir(parents=True, exist_ok=True)
with open(installs_file, "w") as f:
json.dump(installs, f)
else:
thread = threading.Thread(target=load_slow_imports)
thread.daemon = True
thread.start()
except Exception as e:
print(f"Error in check_and_load_imports: {e}", file=sys.stderr)
def load_slow_imports():
# These imports are deferred in various ways to
# improve startup time.
# This func is called in a thread to load them in the background
# while we wait for the user to type their first message.
# This func is called either synchronously or in a thread
# depending on whether it's been run before for this version and executable.
try:
import httpx # noqa: F401
import litellm # noqa: F401
import networkx # noqa: F401
import numpy # noqa: F401
except Exception:
pass
except Exception as e:
print(f"Error in load_slow_imports: {e}", file=sys.stderr)
if __name__ == "__main__":