Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overload modules when using python-asyncio?

I'm using pyinotify to track file changes and try to overload the module where this modified file. But unfortunately, not the module probably is not overloaded, the changes that I am not visible.

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

And code file from the module aa.aa

from aiohttp import web

async def m_aa(request):
   text = b"""
<!DOCTYPE html><meta charset="utf-8" /><html>
 <head></head>
  <body> <h3>Reload</h3> </body>
</html>
       """
   return web.Response(body=text, content_type="text/html")

Maybe there is some other way, I need to change the code did not have to manually reload.

like image 948
alex10 Avatar asked Oct 18 '22 05:10

alex10


1 Answers

You could try aiohttp_autoreload

aiohttp_utils also provides autoreload facility.

like image 174
Andrew Svetlov Avatar answered Oct 21 '22 00:10

Andrew Svetlov