python文件修改检测,检测python模块是否更改然后重新加载

So I'm developing a rather large python project with a number of modules. The "main" (runnable) module is a daemon (a Thrift daemon, actually) which calls off to other modules for its actual functionality. Starting up the daemon takes a long time because some of the modules have a rather lengthy and involved initialization processes.

So when I start the daemon, I wait... let's say... 2 minutes for everything to load, which isn't too bad in the grand scheme of things. However for development it becomes a major pain because I need to restart the daemon EVERY TIME which has been wasting a lot of my time.

Most modules only takes a few seconds to load. Ideally what I'd like to do is detect when any of the files in a particular module have changed, and reload that particular module. I've already figured out how to reload a module, but at this point I can't figure out how to watch a particular module for changes. Keep in mind that a module isn't a single .py file in this case, but rather a directory with __init__.py and 5-10 .py files, so I need to detect when any of them have changed.

Here is the project layout (if it makes any difference at all)

project

| -- daemonize.py

| -- main.py

| -- moduleA

| | -- __init__.py

| | -- happy_panda.py

| ` -- sad_panda.py

| -- moduleB

| | -- __init__.py

| | -- takes_forever_to_load.py

| ` -- seriously_get_some_coffee.py

| -- moduleC

| | -- __init__.py

| | -- frequently_changes.py

| | -- reasons_i_hate_my_job.txt

| ` -- home_address_of_moduleB_developer.txt

` -- service.py

Any ideas or suggestions are appreciated.

EDIT

Thanks for the great feedback. Here is the code I created based on the suggestions. There's a small bug where pyinotify seems to be getting more than one notification, but it's a very small problem for me so I'm not going to fix it.

解决方案

Coupled with you already knowing how to reload your module this answer pretty much fills it out. It uses Inotify to "notify" (see what they did there) the program when the file is modified.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值