2

I would like to embed the great Bottle web framework into a small application (1st target is Windows OS). This app starts the bottle webserver thanks to the subprocess module.

import subprocess
p = subprocess.Popen('python websrv.py')

The bottle app is quite simple

@route("/")
def index():
    return template('index')

run(reloader=True)

It starts the default webserver into a Windows console.

All seems Ok except the fact that I must press Ctrl-C to close the bottle webserver. I would like that the master app terminates the webserver when it shutdowns. I can't find a way to do that (p.terminate() doesn't work in this case unfortunately)

Any idea?

Thanks in advance

luc
  • 37,543
  • 21
  • 117
  • 168
  • If still interested, you may find useful my answer here http://stackoverflow.com/questions/11282218/bottle-web-framework-how-to-stop/16056443#16056443 – mike Jun 18 '13 at 10:04

4 Answers4

4

There are two ways to shutdown a reloading server:

1) You terminate p (using os.kill(p.pid) or p.terminate() ) and then change the modification time of 'websrv.py' (os.utime('websrv.py')) to trigger an automatic shutdown of the child process.

2) You terminate p with os.kill(p.pid, signal.SIGINT) which is identical to a Ctrl-C shutdown.

user242486
  • 226
  • 1
  • 5
1

It seems that the terminate process doesn't work if Bottle is in reload mode. In this case, it starts iteself a subprocess.

If reload is set to False, the terminate seems to work Ok.

luc
  • 37,543
  • 21
  • 117
  • 168
1

Starting with 0.8.1 the reloading server is smart enough to clean up orphan processes. You now have several ways to terminate the server:

  • Hit Ctrl-C or send SIGINT to the parent process. (recommended)
  • Kill the parent process. The child will die gracefully within 2 seconds.
  • Kill the child process or sys.exit() with any status code other than 3. The parent will die immediately.
defnull
  • 3,989
  • 3
  • 22
  • 22
0

I had trouble closing a bottle server from within a request as bottle seems to run requests in subprocesses.

I eventually found the solution was to do:

sys.stderr.close()

inside the request (that got passed up to the bottle server and axed it).

Maybe try doing that in your process and see if bottle gets the message.

karthikr
  • 87,486
  • 24
  • 182
  • 182