如何监控Python文件的变化?
如果代码发生更改,我想重新启动我的 Python Web 应用程序。但是可能有大量文件可以更改,因为导入模块中的文件可能会更改...
如何从导入的包/模块中获取实际的文件名?
如何高效检测修改过的Python文件?有图书馆可以做到这一点吗?
I want to restart my Python web application, if code gets changed. But there could be a large number of files that could be changed, since files in imported modules could change ...
How to get the actual file names from imported packages / modules?
How can modified Python files be detected efficiently? Is there a library to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
无耻的插头。我还正在努力做到这一点 http://github.com/gorakhargosh/watchdog 。
HTH。
Shameless plug. There's also http://github.com/gorakhargosh/watchdog that I'm working on to do exactly this.
HTH.
gamin 是另一个不太特定于 Linux 的选项。
gamin is another option which is slightly less Linux-specific.
我不确定您将如何在您的情况下实现“重新加载应用程序”操作;使用内置的
reload
重新加载已更改的模块可能不会解决问题。但就检测是否发生变化而言,以下是一种处理方法。
sys.modules
中。有时,
__file__
指向一个.pyc
文件而不是.py
文件,因此您可能必须删除尾随的 c。有时.pyc
文件存在,但.py
不存在;在一个强大的系统中,你必须允许这一点。执行此操作的代码概念证明(不可靠):
I'm not sure how you would implement the 'reload application' operation in your circumstance; reloading a changed module with the
reload
built-in probably won't cut it.But as far as detecting whether or not there was a change, the following would be one way to approach it.
__file__
attribute.sys.modules
.sys.modules
at some interval, and look for changes on disk for each module in turnSometimes
__file__
points to a.pyc
file instead of a.py
file, so you might have to chop off the trailing c. Sometimes a.pyc
file exists but a.py
doesn't exist; in a robust system you'd have to allow for this.A proof of concept of code to do this (not robust):
下面是如何使用 pyinotify(即在 Linux 上)实现这一点的示例。
launch_app 调用中的参数是文件名(不带“.py”)、开始执行的函数和以某种方式停止执行的函数。
这是一个可以使用以前的代码(重新)启动的“应用程序”的愚蠢示例:
在您想要使用它的典型应用程序中,您可能会有某种主循环。这是一个更真实的示例,适用于基于 GLib/GTK+ 的应用程序:
相同的概念适用于大多数其他循环(Clutter、Qt 等)。
监视多个代码文件(即属于应用程序一部分的所有文件)和错误恢复(例如,打印异常并在空闲循环中等待,直到代码修复,然后再次启动)留给读者作为练习:) 。
注意:此答案中的所有代码均根据 ISC 许可证(除了知识共享)发布。
Here's an example of how this could be implemented using pyinotify (ie., on Linux).
The parameters in the launch_app call are the filename (without the ".py"), the function to start execution and a function that somehow stops the execution.
Here's a stupid example of an "app" that could be (re-)launched using the previous code:
In a typical application where you'd want to use this, you'd probably have a main loop of some sort. Here's a more real example, for a GLib/GTK+ based application:
The same concept works for most other loops (Clutter, Qt, etc).
Monitoring several code files (ie. all files that are part of the application) and error resilience (eg. printing exceptions and waiting in an idle loop until the code is fixed, then launching it again) are left as exercises for the reader :).
Note: All code in this answer is released under the ISC License (in addition to Creative Commons).
这是特定于操作系统的。对于 Linux,有 inotify,请参见 http://github.com/rvoicilas/inotify-tools/< /a>
This is operating system specific. For Linux, there is inotify, see e.g. http://github.com/rvoicilas/inotify-tools/