pythonfastapiasgipantsnginx-unit

Can a module import of a pex from pantsbuild be exposed?


Cross-post from https://github.com/pantsbuild/pex/issues/1181 as I didn't really know where to ask this.

I'm using Nginx Unit, which has an ASGI configuration for my FastAPI application. It's working fine, but I have no clue how to use it with a PEX.

I've attached it below, but the salient point is that there is a Python plugin for Nginx Unit which is looking for a "module" and a "callable".

What this looks like now is "apigateway.main:app" similar to uvicorn or whatever other server implementation you're using. Unlike uvicorn, gunicorn, or whatever - I don't think I can package any other tool in my pex file and use PEX_SCRIPT.

Is there any suggestion on if/how I can expose a module and variable outside of the pex?

{
    "listeners": {
        "*:80": {
            "pass": "routes"
        }
    },
    "routes": [
        {
            "action": {
                "pass": "applications/api"
            }
        }
    ],
    "applications": {
        "api": {
            "type": "python 3.9",
            "path": "/app",
            "module": "apigateway.main",
            "callable": "app",
            "limits": {
                "requests": 100
            },
            "processes": {}
        }
    },
    "access_log": "/var/log/access.log"
}

Solution

  • This was answered in the Github ticket (https://github.com/pantsbuild/pex/issues/1181):

    All the traditional language here is meant to segue to the recent venv pex tool feature released with Pex 2.1.22. If you build your PEX file with venv support (add --include-tools to the Pex command line) then you gain the ability to create a venv from your PEX file. Doing so is a one-time manual step on the target machine or image:

    PEX_TOOLS=1 ./my.pex venv /app That will create a virtual environment containing your app and its dependencies under /app. You can point Nginx Unit there with home. I've done all this over at https://github.com/jsirois/pex-issues-1181 to prove it works.