squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

Possible runpy.run_path bug? (reload / runner.run) #41

Open ObviouslyGreen opened 7 years ago

ObviouslyGreen commented 7 years ago

Hi,

I've messing around with japronto and have been enjoying it. I realized that app.Run() had a reload flag, so I wanted to try that. When I used the reload flag, it ended up in a infinite loop, constantly reloading, and calling runner.run(). I passed in '_main_' as run_name to the run_path func and it worked, since I had my code in a main function. I tried without the run_name, running in main and running it outside of it. It worked when I didn't have a main.

This happening to anyone else?

With main (infinite loop):

from japronto import Application

def hello(request):
      return request.Response(text='Hello world!')

def main():
    app = Application()
    router = app.router
    app.router.add_route('/', hello)

    app.run(host=host, port=port, reload=True)

if __name__ == '__main__':
    main()

Without main (works fine):

from japronto import Application

def hello(request):
      return request.Response(text='Hello world!')

app = Application()
router = app.router
app.router.add_route('/', hello)

app.run(host=host, port=port, reload=True)

Host and port being imported from another file

It's not a big deal, just removed the if name equal main condition, but I'm just confused why this happened, wondering if anyone can recreate the issue. I tested in both Ubuntu 14.04 and 16.04 both with python 3.6.

edit: as mentioned below, this is default behavior of run_path, I think I might have read the description for run_module or something on accident

squeaky-pl commented 7 years ago

Hi, thanks for reporting this,

This is how Python's run_path works by default, it's easy to fix though and surely entering an infinite loop is not a good thing at all. Also I didn't advertise reload too much in the tutorial for the reason of being a draft implementation. Things to do here:

  1. handle the main case
  2. detect flapping applications, warn, and start backing off algorithm when reloading