reload flag with uvicorn: can we exclude certain code?

9.4k views Asked by At

Is it somehow possible to exclude certain part of the code when reloading the scrip with --reload flag?

uvicorn main:app --reload

Use case: I have a model which takes a lot of time loading so I was wondering if there is a way to ignore that line of code when reloading. Or is it just impossible?

3

There are 3 answers

3
euri10 On BEST ANSWER

Update

Uvicorn now supports including/excluding certain directories/files to/from watchlist.

  --reload-include TEXT           Set glob patterns to include while watching
                                  for files. Includes '*.py' by default, which
                                  can be overridden in reload-excludes.
  --reload-exclude TEXT           Set glob patterns to exclude while watching
                                  for files. Includes '.*, .py[cod], .sw.*,
                                  ~*' by default, which can be overridden in
                                  reload-excludes.

No there is no way to exclude something, however you can be explicit in what you want to be looked at with the --reload-dir flag:

  --reload-dir TEXT               Set reload directories explicitly, instead
                                  of using the current working directory.

in https://www.uvicorn.org/#command-line-options

0
GProst On

I don't believe it's possible, when a server reloads it loads all the needed files into memory from scratch. It doesn't keep the memory of the previous launch.

0
Shirantha Madusanka On

--reload-dir option is available. It will set reload directories explicitly, instead of using the current working directory.

 --reload-dir