首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >asyncore.dispatcher_with_send.send()不返回

asyncore.dispatcher_with_send.send()不返回
EN

Stack Overflow用户
提问于 2015-01-21 14:06:19
回答 1查看 755关注 0票数 0

我有以下代码:

代码语言:javascript
复制
class Handler(asyncore.dispatcher_with_send):
    def __init__(self, class_, sock):
        super().__init__(sock)
        # ...

    # ...

    def writable(self):
        return self.generator or self._out_buffer
    def handle_write(self):
        # ...

        sent = self.send(self._out_buffer)
        import sys
        print(self._out_buffer)
        sys.stdout.flush()
        assert sent is not None
        # ...
        self._out_buffer = self._out_buffer[sent:]
        if not self._out_buffer:
            print('Closing connection.')
            self.close()

    def handle_close(self):
        print('connection closed.')
        super().handle_close()

其产出是:

代码语言:javascript
复制
Incoming connection from ('127.0.0.1', 39045)
b'400 Bad Request\r\n\r\n'
error: uncaptured python exception, closing channel <ppp_libmodule.async_http.Handler connected 127.0.0.1:39045 at 0x7f27c9ab6d30> (<class 'AssertionError'>: [/usr/lib/python3.4/asyncore.py|write|91] [/usr/lib/python3.4/asyncore.py|handle_write_event|461] [/home/progval/.local/lib/python3.4/site-packages/ppp_libmodule-0.7.2-py3.4.egg/ppp_libmodule/async_http.py|handle_write|71])
connection closed.

如您所见,断言sent is not None失败。

但是,根据handle_writeself.send()只应该返回一个整数。而且套接字没有关闭,因为断言失败后调用了handle_close

客户端接收缓冲区中的数据。

知道我做错了什么/不明白吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-21 14:30:13

代码语言:javascript
复制
class Handler(asyncore.dispatcher_with_send):
    ...
    sent = self.send(self._out_buffer)
    assert sent is not None

asyncore.dispatcher_with_send

代码语言:javascript
复制
class dispatcher_with_send(dispatcher):
    ...    
    def send(self, data):
        if self.debug:
            self.log_info('sending %s' % repr(data))
        self.out_buffer = self.out_buffer + data
        self.initiate_send()

dispatcher_with_send.send什么也不返回,所以assert失败了。

示例使用asyncore.dispatcher,其send方法返回发送的字节数。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28069183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档