I have a WebAPI where it is wrapping an external library, the API needs to be able to handle multiple versions of a external library. Two ways I can think of handling this would be to:
I am leaning towards trying to get #2 to work, if it is possible to do, as it will be simpler to deploy.
To do this, I would like to be able to create a FastAPI instance where it can get the version of a package you want to run, then run the function within a process and return the result. A minimal example of this would be:
from fastapi import FastAPI
import numpy as np
app = FastAPI()
@app.get("/{version}/version")
def get_version(version: str) -> str:
"""
This function will create a venv for the numpy version if it doesnt exist,
and then activate that venv and run the code within as a process on that
virtual environment.
"""
return np.__version__
I have a WebAPI where it is wrapping an external library, the API needs to be able to handle multiple versions of a external library. Two ways I can think of handling this would be to:
I am leaning towards trying to get #2 to work, if it is possible to do, as it will be simpler to deploy.
To do this, I would like to be able to create a FastAPI instance where it can get the version of a package you want to run, then run the function within a process and return the result. A minimal example of this would be:
from fastapi import FastAPI
import numpy as np
app = FastAPI()
@app.get("/{version}/version")
def get_version(version: str) -> str:
"""
This function will create a venv for the numpy version if it doesnt exist,
and then activate that venv and run the code within as a process on that
virtual environment.
"""
return np.__version__
Since Python is an interpreted language, each enviroment links to an executable associated with the enviroment, which then interprets the Python code. Have you thought about using subprocess.run () to therefore start the matching executable with the code you want to run as a file parameter?
import subprocess
python_executable = f"{path_to_enviroment}/bin/python"
command = [python_executable, script_path]
result = subprocess.run(command, capture_output=True, text=True)
subprocess.call
), or is that too slow, and you really need all those environments already spun up and just take requests? – deceze ♦ Commented Jan 8 at 13:27subprocess.call
is also fine, speed is not a massive issue – Tom McLean Commented Jan 8 at 13:34