Stay Hungry,Stay Foolish!

watchfiles + importlib == automatical rerun

uvicorn

https://www.uvicorn.org/settings/

此ASGI为开发者模式提供了,代码更改,自动加载功能。

此功能如何实现?

依赖两个库:

watchfiles

importlib

Development

  • --reload - Enable auto-reload. Uvicorn supports two versions of auto-reloading behavior enabled by this option. There are important differences between them.
  • --reload-dir <path> - Specify which directories to watch for python file changes. May be used multiple times. If unused, then by default the whole current directory will be watched. If you are running programmatically use reload_dirs=[] and pass a list of strings.

watchfiles

https://watchfiles.helpmanual.io/

 

Simple, modern and high performance file watching and code reload in python.

Underlying file system notifications are handled by the Notify rust library.

This package was previously named "watchgod", see Migrating from watchgod for more information.

 

from watchfiles import watch

for changes in watch('./path/to/dir'):
    print(changes)

 

importlib

https://docs.python.org/3/library/importlib.html

importlib.reload(module)

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (which can be different if re-importing causes a different object to be placed in sys.modules).

 

posted @ 2023-10-29 22:58  lightsong  阅读(6)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel