aio-libs / aiohttp-devtools

dev tools for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation how to add livereload by api

sky-code opened this issue · comments

Please add example how to add livereload from code, without cli usage.
I have runserver.py script that up and running aiohttp web server

"""
This script runs the natrix application using a development server.
"""
from os import environ
from myapp import run_app

if __name__ == '__main__':
    HOST = environ.get('SERVER_HOST', '0.0.0.0')
    try:
        PORT = int(environ.get('SERVER_PORT', '8888'))
    except ValueError:
        PORT = 5555
    run_app(HOST, PORT) # create web.Application, setup middlewares and routes and call web.run_app

How can I add livereload here?
Will be good to add some utility function which will do all the work for attach live reload
as example:

from aiohttp_devtools import patch_web_app
patch_web_app(web_app)

It's not trivially because aiohttp-devtools is designed to run two apps, your code on 8000, and the "aux app" on 8001 which includes livereload and static file serving.

This is so your code can be reloaded without killing the livereload websocket connections.

Therefore this package doesn't have the concept of patching an app to add livereload.

Can you explain why you're not able to use the cli or (almost the same) call runserver?

I want to run my app from PyCharm for debug and other PyCharm features, because of that I don't use cli, maybe I can use cli and get full integration with PyCharm, but I don't know how to do this.

call runserver? I will try.

(since cli and runserver both call run your app in a subprocess using multiprocessing.Process I wouldn't be surprised if this still doesn't work)

Almost done, but I get an exception

Traceback (most recent call last):
  File "/Users/macbook/virtualenvs/natrix_env/bin/adev", line 11, in <module>
    sys.exit(cli())
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/cli.py", line 88, in runserver
    run_app(*_runserver(**active_config))
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/main.py", line 48, in runserver
    config.check(loop)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/config.py", line 179, in check
    loop.run_until_complete(self._startup_cleanup(loop))
  File "/Users/macbook/.pyenv/versions/3.6.3/lib/python3.6/asyncio/base_events.py", line 467, in run_until_complete
    return future.result()
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/config.py", line 200, in _startup_cleanup
    app = self.load_app(loop)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/config.py", line 188, in load_app
    app = self.app_factory(loop=loop)
  File "/Users/macbook/work/natrix/natrix/web/main.py", line 30, in create_app
    app = loop.run_until_complete(init(loop))
  File "/Users/macbook/.pyenv/versions/3.6.3/lib/python3.6/asyncio/base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "/Users/macbook/.pyenv/versions/3.6.3/lib/python3.6/asyncio/base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

that's a problem with your code (or at least a conflict with how aiohttp-devtools works), I can't really help.

(You can post the code web/main.py here and I'll fix if it short and simple)

web/main.py file

import asyncio

from aiohttp import web

from natrix.core.app import bootstrap_app
from natrix.web import WEB_DIR_PATH
from natrix.web.middlewares import setup_middlewares
from natrix.web.routes import setup_routes


async def init(loop):
    # setup application and extensions
    app = web.Application(loop=loop)
    index_html_path = WEB_DIR_PATH.joinpath('static', 'index.html')
    try:
        with open(index_html_path, encoding='utf-8') as f:
            app['index.html'] = f.read()
    except Exception as e:
        app['index.html'] = str(e)

    # setup views and routes
    setup_routes(app, WEB_DIR_PATH)
    setup_middlewares(app)

    return app


def create_app(loop):
    bootstrap_app()
    app = loop.run_until_complete(init(loop))
    return app


def run_app(host, port):
    loop = asyncio.get_event_loop()
    app = create_app(loop)
    web.run_app(app, host=host, port=port)

init doesn't need to be async, just remove async and loop.run_until_complete().

If you do need to do any async work before starting the app use on_startup

yep this solve previous problem, but anyway i get an exception:

[19:33:05] Starting aux server at http://localhost:8001 ◆
2017-10-19 19:33:05,120 - adev.server.dft - INFO - Starting aux server at http://localhost:8001 ◆
[19:33:05] Starting dev server at http://localhost:8000 ●
2017-10-19 19:33:05,122 - adev.server.dft - INFO - Starting dev server at http://localhost:8000 ●
Process Process-1:
Traceback (most recent call last):
  File "/Users/macbook/.pyenv/versions/3.6.3/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/Users/macbook/.pyenv/versions/3.6.3/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/serve.py", line 108, in serve_main_app
    with set_tty(tty_path):
  File "/Users/macbook/.pyenv/versions/3.6.3/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/serve.py", line 99, in set_tty
    with open(tty_path) as tty:
OSError: [Errno 6] Device not configured: '/dev/tty'

This was caused by fixing #118, I guess you're on OSX?

I have no idea what the standard stdin tty file path for OSX is.

yep, OSX latest version. What I need to fix this?

What do you get if you run tty in the terminal?

What do you get if you run os.ttyname(sys.stdin.fileno()) in python?

what happens if you run adev runserver ... directly?

adev runserver from terminal starts without any error, application work but sometimes throws an exception:

raceback (most recent call last):
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 416, in start
    resp = yield from self._request_handler(request)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp/web.py", line 323, in _handle
    resp = yield from handler(request)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/serve.py", line 65, in _handler
    return await handler(request)
  File "/Users/macbook/work/natrix/natrix/web/middlewares.py", line 18, in middleware_handler
    response = await handler(request)
  File "/Users/macbook/work/natrix/natrix/web/views.py", line 99, in projects
    projects_ = await db.get_project_list(take=take)
  File "/Users/macbook/work/natrix/natrix/core/db.py", line 79, in get_project_list
    .to_list(length=take)
RuntimeError: Task <Task pending coro=<RequestHandler.start() running at /Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp/web_protocol.py:416>> got Future <Future pending> attached to a different loop
2017-10-19 19:48:10,980 - adev.server.dft - ERROR - Error handling request
Traceback (most recent call last):
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 416, in start
    resp = yield from self._request_handler(request)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp/web.py", line 323, in _handle
    resp = yield from handler(request)
  File "/Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp_devtools/runserver/serve.py", line 65, in _handler
    return await handler(request)
  File "/Users/macbook/work/natrix/natrix/web/middlewares.py", line 18, in middleware_handler
    response = await handler(request)
  File "/Users/macbook/work/natrix/natrix/web/views.py", line 99, in projects
    projects_ = await db.get_project_list(take=take)
  File "/Users/macbook/work/natrix/natrix/core/db.py", line 79, in get_project_list
    .to_list(length=take)
RuntimeError: Task <Task pending coro=<RequestHandler.start() running at /Users/macbook/virtualenvs/natrix_env/lib/python3.6/site-packages/aiohttp/web_protocol.py:416>> got Future <Future pending> attached to a different loop

tty from terminal

MacBook-Pro-Macbook:~ macbook$ tty
/dev/ttys002

system python

os.ttyname(sys.stdin.fileno())
'/dev/ttys002'

from pycharm launcher

Traceback (most recent call last):
  File "/Users/macbook/work/natrix/ty.py", line 5, in <module>
    print(os.ttyname(sys.stdin.fileno()))
OSError: [Errno 25] Inappropriate ioctl for device

from my project venv

python ty.py 
/dev/ttys003

i gues problem in PyCharm

The first error should be fixed by #150, if you can confirm that's fixed I will release it.

tty problem is related to pycharm as you suggested: it does something string to stdin, I can fix that, however I still doubt that pycharm's debugger will work properly sine your app and aiohttp-devtools due to the subprocess, we can try though.

I will check it tomorrow

#152 should fix the stdin error, with that pycharm is working well for me.