snower / TorMySQL

The highest performance asynchronous MySQL driver by PyMySQL
MIT License
308 stars 63 forks source link

IOStream read_bytes 读长度错误 #9

Closed zhu327 closed 8 years ago

zhu327 commented 8 years ago

我尝试在我的greentor中使用你重写的IOStream的,发现会有读长度错误,_read_bytes小于_read_buffer_size的时候,会返回整个_read_buffer而不是_read_bytes长度的buff,fix的代码如下

    def read_bytes(self, num_bytes):
        assert self._read_future is None, "Already reading"
        if self._closed:
            raise StreamClosedError(real_error=self.error)

        future = self._read_future = Future()
        self._read_bytes = num_bytes
        self._read_partial = False
        if self._read_buffer_size >= self._read_bytes:
            future, self._read_future = self._read_future, None
            data = b"".join(self._consume(self._read_bytes))
            self._read_bytes = 0
            future.set_result(data)
        return future

_handle_read方法下面同样的代码也需要改下

snower commented 8 years ago

这个是故意这么设计的,你看Connection的调用点,减少调用次数,确保返回_read_bytes长度是由Connection的read_bytes保证的

zhu327 commented 8 years ago

明白了,你在Connection里面又维护了一个buffer,我使用的必须要修改

snower commented 8 years ago

嗯,可以减少携程切换