玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容

请问Python tornado异步tcp服务器一些问题

大家好!
我有一段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()就要开始读了?我要怎样去解决这样的冲突呢

已解决 http://stackoverflow.com/questions/31344127/how-to-process-multiple-co...

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/wenda/wd18482.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年06月10日 - 19时37分12秒 发布自玩蛇网

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules