使用 python asyncore 从套接字读取固定数量的字节
我使用 asyncore 使用“length:message”类型的协议与远程服务器进行通信。有人可以推荐我一种从套接字读取确切字节数的方法吗?我试图使用handle_read来填充内部缓冲区并每次调用我的函数,检查缓冲区的大小,但它看起来太丑陋(检查缓冲区是否足够长以读取长度,检查缓冲区长度是否大于消息长度,读取消息等)。是否有可能有类似“socket.read(bytes)”的东西,它会休眠直到缓冲区填满并返回值?
I use asyncore to communicate with remote servers using "length:message"-type protocol. Can someone recommend me a way to read exact amount of bytes from socket? I was trying to use handle_read to fill internal buffer and call my function every time, checking for size of buffer, but it looked too ugly(Check if buffer is long enough to read length, check if buffer length is bigger than message length, read message, etc...). Is it possible to have something like "socket.read(bytes)" which would sleep until buffer is filled enough and return value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。睡眠会破坏异步 IO 的全部目的。
然而,使用 twisted 做到这一点非常简单。
No. Sleeping would defeat the entire purpose of asynchronous IO.
However, this is remarkably simple to do with twisted.