我如何让我的龙卷风ioloop套接字客户端在收到后发回回复?

时间:2013-08-03 22:35:22

标签: python asynchronous tornado

iv创建了一个简单的异步客户端和服务器,但我第一次收到后无法让客户端回复。似乎服务器可以在从客户端接收后发回回复,但客户端不能:

这是客户的会话:

[mike@mike Public]$ python cl.py 
buf got your stuff
dded callback           ## this is a log  addded  to see if execution got where i wanted

这是服务器的日志:

[mike@mike Public]$ python that.py 
buf ehlo localhost

我期待某种乒乓效应,其中一个发送然后另一个然后冲洗泡沫重复。

这是客户的代码:

import socket
import fcntl, os, io, time, functools
from tornado import ioloop


class Punk(object):
     def  __init__(self):
         self.loop = ioloop.IOLoop.instance()
         self.address = 'blah.sock'
         self.authkey = "none"
         self.sock  = socket.socket(socket.AF_UNIX)
     def setup(self):

         self.sock.connect(self.address)
         fcntl.fcntl(self.sock, fcntl.F_SETFL, os.O_NONBLOCK)
         self.sock.sendall("ehlo localhost")
         self.fd = self.sock.fileno()
         self.loop.add_handler(self.fd,self.reader,self.loop.READ)
         self.loop.start()

     def reader(self,fd,event):
         result = b""
         if event == self.loop.READ:

             try:
                 while True:
                     servrep = self.sock.recv(1024)
                     if not servrep:
                         break
                     result += servrep
                     self.prnt(result)
                     break
             except Exception as e:
                 print "this %s happend"%e
         return

     def prnt(self,buf):
         print "buf %s"%buf
         tim =  time.time() + 2
         self.loop.instance().add_timeout(tim, self.reply)
         #callbac = functools.partial(self.loop.add_timeout,tim,self.reply)
         #self.loop.add_callback(self.reply)    ### i tried this too
         print "added callback"


     def reply(self):
         self.sock.sendall(" clent got your stuff")

if __name__ == "__main__":
     bob = Punk()
     bob.setup()

这是服务器:

import socket
import fcntl, os, io, time, functools
from array import array
from tornado import ioloop 


class Player(object):
     def  __init__(self):
         self.loop = ioloop.IOLoop.instance()
         self.address = 'blah.sock'
         self.authkey = "none"
         self.sock  = socket.socket(socket.AF_UNIX)
     def setup(self):
         self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
         self.sock.bind(self.address)
         fcntl.fcntl(self.sock, fcntl.F_SETFL, os.O_NONBLOCK)
         self.sock.listen(1)
         self.fd = self.sock.fileno()
         self.loop.add_handler(self.fd,self.reader,self.loop.READ)
         self.loop.start()

     def reader(self,fd,event):
         result = b""
         if event == self.loop.READ:
             self.conn, self.addr = self.sock.accept() 
             try:
                 while True:
                     maxrep = self.conn.recv(1024)
                     if not maxrep:
                         break
                     result += maxrep
                     self.prnt(result)
                     break
             except Exception as e:
                 print "this %s happend"%e
         return

     def prnt(self,buf):
         print "buf %s"%buf
         tim = time.time() + 2
         self.loop.instance().add_timeout(tim, self.reply) 
         #callbac = functools.partial(self.loop.add_timeout,tim,self.reply)
         #self.loop.add_callback(callbac)


     def reply(self):
         self.conn.sendall("got your stuff")

if __name__ == "__main__":
     bob = Player()
     bob.setup()

1 个答案:

答案 0 :(得分:0)

我已将套接字设置为非阻塞模式,但在接受时我没有收到错误 没有连接时的非阻塞状态: 这里:

def reader(self,fd,event):
     result = b""
     if event == self.loop.READ:
         self.conn, self.addr = self.sock.accept() 

应该是

 def reader(self,fd,event):
     result = b""
     if event == self.loop.READ:
         try:
             self.conn, self.addr = self.sock.accept() # we get stuck here
             self.connl.append(self.conn)
         except Exception as e:
             pass