Python的asyncore客户端将定期数据发送到服务器
我需要连接到服务器,例如smpp服务器,并每隔2秒发送定期数据,以下是代码:Python的asyncore客户端将定期数据发送到服务器,python,multithreading,client,asyncore,Python,Multithreading,Client,Asyncore,我需要连接到服务器,例如smpp服务器,并每隔2秒发送定期数据,以下是代码: import asyncore, socket, threading, time class SClient(asyncore.dispatcher): buffer = "" t = None def __init__(self, host): asyncore.dispatcher.__init__(self) self.create_socket(soc
import asyncore, socket, threading, time
class SClient(asyncore.dispatcher):
buffer = ""
t = None
def __init__(self, host):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect( (host, 25) )
print "sending data from __init__"
self.sendCommand("data_init")
self.t = SenderThread(self)
self.t.start()
def sendCommand(self, command):
self.buffer = command
def handle_close(self):
self.close()
self.t.stop()
def handle_read(self):
print self.recv(8192)
def writable(self):
print 'asking for writable ? len='+str(len(self.buffer))
return (len(self.buffer) > 0)
def handle_write(self):
print "writing to socket"
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
print "wrote "+str(sent)+" to socket"
class SenderThread(threading.Thread):
_stop = False
def __init__(self, client):
super(SenderThread,self).__init__()
self.client = client
def stop(self):
self._stop = True
def run(self):
counter = 0
while self._stop == False:
counter += 1
time.sleep(1)
if counter == 2:
print "sending data from thread"
self.client.sendCommand("data_thread")
counter = 0
client = SClient('127.0.0.1')
asyncore.loop()
以下是运行时的输出:
$ python test.py
sending data from __init__
asking for writable ? len=9
writing to socket
wrote 9 to socket
asking for writable ? len=0
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
asking for writable ? len=11
writing to socket
wrote 11 to socket
asking for writable ? len=0
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
asking for writable ? len=11
writing to socket
wrote 11 to socket
asking for writable ? len=0
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
sending data from thread
我的线程每2秒通过buffer变量向服务器发送一次数据,但asyncore每1分钟调用writable和handle_write,我不明白为什么它在从线程填充缓冲区后不立即提取缓冲区?查看文档中的方法
timeout参数为相应的
选择或轮询呼叫,以秒为单位;默认值为30
秒
它只是每隔30秒触发一次句柄写入。但是当从SClient对象内部调用sendCommand时,asyncore会检测到缓冲区不是空的,并实时将数据发送到服务器。这不是我读取文档的方式。每次围绕异步循环调用writeable方法,以确定是否应将通道的套接字添加到可发生写事件的列表中。在示例代码中,尝试将超时设置为2秒。在轮询之前,您有15个send命令*2秒。