def run_with_reloader(loop, coroutine, cleanup=None, *args, **kwargs):
""" Run coroutine with reloader """
clear_screen()
print("?? Running in debug mode with live reloading")
print(" (don't forget to disable it for production)")
# Create watcher
handler = Handler(loop)
watcher = Observer()
# Setup
path = realpath(os.getcwd())
watcher.schedule(handler, path=path, recursive=True)
watcher.start()
print(" (watching {})".format(path))
# Run watcher and coroutine together
done, pending = await asyncio.wait([coroutine, handler.changed],
return_when=asyncio.FIRST_COMPLETED)
# Cleanup
cleanup and cleanup()
watcher.stop()
for fut in done:
# If change event, then reload
if isinstance(fut.result(), Event):
print("Reloading...")
reload()
评论列表
文章目录