Hello, I'm trying to develop an api for my project using your framework but every time I save the changes I need to close and open server again, and it does it everytime, is there any way to make livereload in sanic?.
Did some experimenting on auto-reloading on changes of the code to ease the development cycle. I can correctly detect a change in the project-files (using python watchdog) but how can I restart the whole server? Calling get_event_loop().stop() from the file-has-changed handler just gives: RuntimeError: There is no current event loop in thread 'Thread-1'.
I think livereload is useful in develop!
I have made a live reload library AoikLiveReload that is applicable to most of Python programs (and to crash a few of them). Here is the demo for reloading Sanic server.
Install the library with:
pip install AoikLiveReload
Add the 3 lines to your code:
from aoiklivereload import LiveReloader
reloader = LiveReloader()
reloader.start_watcher_thread()
Now when there is a module file change, the program will be reloaded.
Rolling into 0.2.0
@seemethere When to release 0.2.0
@istommao There's no official time-table but it's going to be focused on this change as well as the configuration and logging changes.
Read-as: It'll be released once those features are in
Going to cut a release for 0.2.0 today, will move this to the next milestone.
Just adding some thoughts on live reload.
Can we do this without adding watchdog to the requirements or setup file? Code running in production should have as little dependencies as possible and a development dependency should never go into production. Perhaps at the start of a debug server a check can be made to see if watchdog is supported, if so the dev server gets livereload otherwise a message along the lines of to enable live reload run "pip install watchdog".
I would also recommend the Flask approach of preferencing the cli to facilitate live reload rather than putting the reload code into the equivalent ofSanic.run. Theres a stack overflow discussion with some insight into the decision. But two standout features of the cli approach in my opinion are:
Here is a minimal reloader that I've been using, theres a lot missing, but I'm putting it here because I'd just like to draw attention to using multiprocessing rather than lower level process management:
from multiprocessing import Process
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Reloader:
class Handler(FileSystemEventHandler):
def __init__(self, reloader):
self.reloader = reloader
def on_any_event(self, event):
self.reloader.reload()
def __init__(self, directory, callback):
self.observer = None
self.process = None
self.handler = Reloader.Handler(self)
self.directory = directory
self.callback = callback
def watch(self, first=True):
if first:
self.reload()
else:
self.observer = Observer()
self.observer.schedule(self.handler, self.directory, recursive=True)
self.observer.start()
def reload(self):
if self.observer:
self.observer.stop()
self.process.terminate()
self.process.join()
try:
self.process = Process(target=self.callback)
self.process.start()
finally:
self.watch(first=False)
def run_server():
from my_app import MyApp
app = MyApp()
app.run(host='0.0.0.0', port=5000, debug=True)
Reloader('my_app', run_server).watch()
while True:
time.sleep(1)
@atbentley Actually I think that is a really smart way to handle it. We could handle live reload as a flag to the CLI interface and then raise an exception if they don't have watchdog installed.
Then only question that remains is what exactly should we watch for. Some servers with live reload choose to watch not only the app directory but also all of the site-packages as well. But if we go that route I could see the live reload watcher getting intensive very quickly.
The watchdog docs provide some implementation details, OS X / macOS and BSD should be ok for performance, I'm not sure about Linux and WIndows. If performance is an issue though a non-recursive watch could be placed on site-packages which would catch installs and uninstalls but not upgrades or manual edits to packages.
I might have an attempt at implementing this over the next few days if no one else has a branch going already.
You might want to look into https://github.com/channelcat/sanic/pull/242 https://github.com/channelcat/sanic/pull/242 first..
—
On 25 Jan 2017, at 11:44, Andrew Bentley notifications@github.com wrote:
The watchdog docs http://pythonhosted.org/watchdog/installation.html#supported-platforms-and-caveats provide some implementation details, OS X / macOS and BSD should be ok for performance, I'm not sure about Linux and WIndows. If performance is an issue though a non-recursive watch could be placed on site-packages which would catch installs and uninstalls but not upgrades or manual edits to packages.
I might have an attempt at implementing this over the next few days if no one else has a branch going already.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/channelcat/sanic/issues/168#issuecomment-275076001, or mute the thread https://github.com/notifications/unsubscribe-auth/ADfU2A71bTtynmIS51Bs8-F-x_1wdAcRks5rVyd3gaJpZM4K8iaA.
I really want this feature. In my opinion, Flask way is good enough.
I think that it is appropriate to provide this function by extensions,
like the nodemon for Node.js.
Another workaround is using entr :
find src/ -name \*.py | entr -r python app.py
@sorrat Actually this is the way I recommended in the WIP commit that I had up.
It should probably find it's way into documentation...
is it really necessary ? i'd suggest use gunicorn, it's pretty simple.
And it's very helpful for local development to integrate sanic with some python build systems.
I'm using hupper, and it works great with sanic.
@sorrat That works, however it does not support pdb; pdb.set_trace()
Thanks to @Anton-2 Hupper works well!
I've written an sanic dev entry script for ease, which also has a --shell option bring you into an ipython shell after server start.
I'm using sanic-admin for hot reloading with success...
+1 for this feature, there are situations where you don't want to use gunicorn and entr does not work either. It would be really amazing if this was built in as it is a common feature in modern frameworks and adds very little weight to the library.
Did some kind of auto-reload get into sanic? It's hard to tell where this discussion ended up.
Could anyone tell me what's the status for this issue?
Auto-reloading was added in https://github.com/channelcat/sanic/commit/52c2a8484e6aa5fa13aaade49e1f2597dd006e15
You may enable by either passing debug=True or auto_reload=True to app.run
Thanks, I'll wait for the next release to get this feature
See also #1248; even in next release you will need to start the server using an external script to make reload work.
Most helpful comment
I have made a live reload library AoikLiveReload that is applicable to most of Python programs (and to crash a few of them). Here is the demo for reloading Sanic server.
Install the library with:
Add the 3 lines to your code:
Now when there is a module file change, the program will be reloaded.