加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用python-asyncio时如何重载模块?

发布时间:2020-12-16 22:47:40 所属栏目:Python 来源:网络整理
导读:我正在使用pyinotify来跟踪文件更改并尝试重载此修改文件所在的模块. 但不幸的是,不是模块可能没有超载,我看不到变化. import sysimport asyncioimport pyinotifyimport importlibfrom aiohttp import webfrom aa.aa import m_aaclass EventHandler(pyinotif

我正在使用pyinotify来跟踪文件更改并尝试重载此修改文件所在的模块.
但不幸的是,不是模块可能没有超载,我看不到变化.

import sys
import asyncio
import pyinotify
import importlib
from aiohttp import web
from aa.aa import m_aa


class EventHandler(pyinotify.ProcessEvent):

   def my_init(self,loop=None):
       self.loop = loop if loop else asyncio.get_event_loop()

    def process_IN_MODIFY(self,event):
       pathname = event.pathname
       name = event.name
       if name.endswith('.py'):
            for module in sys.modules.values():
               if hasattr(module,'__file__'):
                   if module.__file__ == pathname:
                       importlib.reload(module)

def inotify_start(loop):
   wm = pyinotify.WatchManager()
   wm.add_watch('/home/test',pyinotify.ALL_EVENTS,rec=True)
   handler = EventHandler( loop=loop )
   pyinotify.AsyncioNotifier(wm,loop,default_proc_fun=handler)


async def init(loop):
   app = web.Application()
   app.router.add_route('GET','/',m_aa)
   handler = app.make_handler()
   inotify_start(loop)
   srv = await loop.create_server(handler,'0.0.0.0',8080)
   return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
   loop.run_forever()
except KeyboardInterrupt:
   pass

和来自模块aa.aa的代码文件

from aiohttp import web

async def m_aa(request):
   text = b"""

也许还有其他一些方法,我需要更改代码而不必手动重新加载.

最佳答案
你可以尝试aiohttp_autoreload

aiohttp_utils还提供自动重载功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读