utengr
utengr

Reputation: 3355

reload flag with uvicorn: can we exclude certain code?

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?

Upvotes: 19

Views: 12939

Answers (4)

Roger Camargo
Roger Camargo

Reputation: 66

The watchfiles library used for the uvicorn tries to check the excluded folders to see if it is a dir or not...in this case it will fail... https://github.com/encode/uvicorn/blob/master/uvicorn/supervisors/watchfilesreload.py#L27

In this meanwhile, using the variable WATCHFILES_IGNORE_PERMISSION_DENIED=0 at least the error below stops:

  File ".../site-packages/watchfiles/main.py", line 118, in watch
    with RustNotify(
         ^^^^^^^^^^^
PermissionError: Permission denied (os error 13) about ["../myproject/postgres_data"]

Upvotes: 1

Shirantha Madusanka
Shirantha Madusanka

Reputation: 1695

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

 --reload-dir 

Upvotes: -1

euri10
euri10

Reputation: 2626

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

Upvotes: 23

GProst
GProst

Reputation: 10237

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.

Upvotes: 0

Related Questions