关于tornado异步tcp的问题

发布于 2022-09-01 15:17:43 字数 2337 浏览 12 评论 0

大家好!
我有一段tornado异步tcp服务器的代码,tcp服务器向客户端发送命令,遇到一些问题.

#! /usr/bin/env python
#coding=utf-8

from tornado.tcpserver import TCPServer
from tornado.ioloop import IOLoop
from ctypes import *
import struct
from const import *
from systemModel import *


class TcpConnection(object):
    def __init__(self,stream,address):
        self._stream=stream
        self._address=address
        self._stream.set_close_callback(self.on_close)
        self.getSystemInfo()
        self.getSystemInfo()


    def on_close(self):
        print("the monitored %d has left",self._address)

    def getSystemInfo(self):
        commandType=Command.SYSTEMINFO
        commandInfo=Command.NOCOMMANDINFO
        data=struct.pack(FormatStr.COMMANDINFO,commandType,commandInfo.encode('utf-8'))
        self._stream.write(data)
        self._stream.read_bytes(SystemInfo.len_whole,setSystemInfo)


class MonitorServer(TCPServer):
    def handle_stream(self,stream,address):
        print("new connection",address,stream)
        TcpConnection(stream,address)


if  __name__=='__main__':
    print('server start .....')
    server=MonitorServer()
    server.listen(20000)
    IOLoop.instance().start()

上面有2个self.getSystemInfo(),我的意思是想,能否一下丢出几个命令,比如传文件,传信息等.但是上面的代码会报错.错误信息如下:

ERROR:tornado.application:Error in connection callback
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/tcpserver.py", line 269, in _handle_connection
    future = self.handle_stream(stream, address)
  File "/home/zz/PycharmProjects/monitor/server.py", line 64, in handle_stream
    TcpConnection(stream,address)
  File "/home/zz/PycharmProjects/monitor/server.py", line 18, in __init__
    self.getSystemInfo()
  File "/home/zz/PycharmProjects/monitor/server.py", line 32, in getSystemInfo
    self._stream.read_bytes(SystemInfo.len_whole,setSystemInfo)
  File "/usr/local/lib/python3.4/dist-packages/tornado/iostream.py", line 303, in read_bytes
    future = self._set_read_callback(callback)
  File "/usr/local/lib/python3.4/dist-packages/tornado/iostream.py", line 658, in _set_read_callback
    assert self._read_callback is None, "Already reading"
AssertionError: Already reading

这个错误信息,是不是前一个self.getSystemInfo()还没读完,后一个self.getSystemInfo()就要开始读了?我要怎样去解决这样的冲突呢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文