0

I am building an application using the Bottle web-framework.

I would like to catch signals USR1 and USR2 to do some work aside from the bottle server. Mainly I want to be able to reload configuration without shutting down the web server because I want some objects to live on.

I tried to handle the signals (USR1 and USR2) on my own using :

signal.signal(signal.SIGUSR1, my_handler)

The problem is that, upon receiving a sigUSR1, the bottle web server crashes with the following trace :

Traceback (most recent call last):
  File "giomanager.py", line 46, in <module>
    run( giomanager, port=60200 )
  File "/usr/lib/python2.7/dist-packages/bottle.py", line 2389, in run
    server.run(app)
  File "/usr/lib/python2.7/dist-packages/bottle.py", line 2087, in run
    srv.serve_forever()
  File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
    r, w, e = select.select([self], [], [], poll_interval)
select.error: (4, 'Interrupted system call')

Do you have an idea why is this happening ? Is it possible to prevent bottle from receiving those signals ?

4

1 回答 1

0

你没有发布你的代码,所以我猜它可能归结为这样的事情:

import signal
from bottle import route, run

def my_handler(*args):
    print 'in signal handler', args

signal.signal(signal.SIGUSR1, my_handler)

@route('/hello')
def hello():
    return "Hello World!\n"

run(host='localhost', port=8080, debug=True)

顺便说一句,这对我有用——它打印“在信号处理程序中”。

但是:我真正想建议的是,您完全可以考虑另一种机制来刷新数据。我在一个单独的刷新线程上取得了很大的成功,该线程反复休眠一段时间,然后醒来并轮询某个文件(或 url)以查看是否有新数据。

这可能比使用信号更好的原因有几个,其中最重要的一点是,如果您曾经运行多进程,向每个进程发出信号会很痛苦(或不可能)。此外,您不打算在生产环境中运行 Bottle 开发服务器,是吗?(除了最小的应用程序之外,不建议使用它。)鉴于此,一旦您在另一个网络服务器中运行您的 Bottle 应用程序,信号处理就会变得棘手。

于 2013-05-01T00:45:14.303 回答