捕获瓶服务器错误 [英] Catching bottle server errors

查看:88
本文介绍了捕获瓶服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装我的瓶子服务器,以便当游戏中的一个人注销时,每个人都可以立即看到它.当我使用长时间轮询时,所有用户都会打开一个请求.

I am trying to get my bottle server so that when one person in a game logs out, everyone can immediately see it. As I am using long polling, there is a request open with all the users.

我遇到的麻烦是捕获了用户从无法再连接到该页面的长时间轮询中离开该页面时引发的异常.错误消息在这里.

The bit I am having trouble with is catching the exception that is thrown when the user leaves the page from the long polling that can no longer connect to the page. The error message is here.

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gevent/pywsgi.py", line 438, in handle_one_response
    self.run_application()
  File "/usr/lib/python2.7/dist-packages/gevent/pywsgi.py", line 425, in run_application
    self.process_result()
  File "/usr/lib/python2.7/dist-packages/gevent/pywsgi.py", line 416, in process_result
    self.write(data)
  File "/usr/lib/python2.7/dist-packages/gevent/pywsgi.py", line 373, in write
    self.socket.sendall(msg)
  File "/usr/lib/python2.7/dist-packages/gevent/socket.py", line 509, in sendall
    data_sent += self.send(_get_memory(data, data_sent), flags)
  File "/usr/lib/python2.7/dist-packages/gevent/socket.py", line 483, in send
    return sock.send(data, flags)
error: [Errno 32] Broken pipe
<WSGIServer fileno=3 address=0.0.0.0:8080>: Failed to handle request:
  request = GET /refreshlobby/1 HTTP/1.1 from ('127.0.0.1', 53331)
  application = <bottle.Bottle object at 0x7f9c05672750>

127.0.0.1 - - [2013-07-07 10:59:30] "GET /refreshlobby/1 HTTP/1.1" 200 160 6.038377

处理该页面的功能是这个.

The function to handle that page is this.

@route('/refreshlobby/<id>')
def refreshlobby(id):
    while True:
        yield lobby.refresh()
        gevent.sleep(1)

我试图在函数中以及在我包装@route的装饰器中捕获异常,但两者都不起作用.我尝试制作一个@error(500)装饰器,但是也没有触发.看来这与瓶子的内部有关.

I tried catching the exception within the function, and in a decorator which I put to wrap @route, neither of which worked. I tried making an @error(500) decorator, but that didn't trigger, either. It seems that this is to do with the internals of bottle.

我现在知道我需要捕获socket.error,但是我不知道代码中的下落

I know now that I need to be catching socket.error, but I don't know whereabouts in my code

推荐答案

WSGI运行器

仔细查看回溯:这不是在您的函数中发生,而是在WSGI运行程序中发生.

The WSGI runner

Look closely at the traceback: this in not happening in your function, but in the WSGI runner.

Traceback (most recent call last):
    File "/usr/lib/python2.7/dist-packages/gevent/pywsgi.py", line 438, in handle_one_response
        self.run_application()

在您的情况下,WSGI运行器的工作方式是:

The way the WSGI runner works, in your case, is:

  1. 接收请求
  2. 从您的代码中获得部分响应
  3. 将其发送给客户端(这是引发异常的地方)
  4. 重复步骤2-3

您无法捕获此异常

您的代码中未引发此错误.

You can't catch this exception

This error is not raised in your code.

当您尝试向关闭连接的客户端发送响应时,就会发生这种情况.

It happens when you try to send a response to a client that closed the connection.

因此,您将无法从代码中捕获此错误.

You'll therefore not be able to catch this error from within your code.

不幸的是,无法从生成器(您的代码)中得知何时停止使用它.

Unfortunately, it's not possible to tell from within the generator (your code) when it stops being consumed.

依靠生成器进行垃圾回收也不是一个好主意.

It's also not a good idea to rely on your generator being garbage collected.

您还有其他两种解决方案.

You have a couple other solutions.

知道用户何时断开连接的另一种方法可能是在您的yield语句之后记录最后一次看到".

Another way to know when an user disconnects would probably be to record a "last seen", after your yield statement.

如果最近一次断开连接的客户端已经过去,您将能够确定它们已断开连接.

You'll be able to identify clients that disconnected if their last seen is far in the past.

另一个非WSGI运行程序将更适合于实时应用程序.您可以尝试tornado.

Another, non-WSGI runner, will be more appropriate for a realtime application. You could give tornado a try.

这篇关于捕获瓶服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆