简体   繁体   English

Python 的异步客户端将定期数据发送到服务器

[英]Python's asyncore client to send periodic data to server

I need to connect to a server (eg smpp server) and send periodic data every 2 seconds, here's the code:我需要连接到服务器(例如 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(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()

Here's the output when running:这是运行时的output:

$ 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

My thread is sending data to server every 2 seconds through the buffer variable but asyncore is calling writable and handle_write exactly every 1 minute, i dont understand why it doesnt fetch the buffer just after its getting populated from the thread?我的线程通过缓冲区变量每 2 秒向服务器发送一次数据,但是 asyncore 每 1 分钟准确地调用一次可写和句柄写入,我不明白为什么它在从线程中填充后不获取缓冲区?

Look at the docs for the asyncore loop method.查看asyncore 循环方法的文档。

The timeout argument sets the timeout parameter for the appropriate select() or poll() call, measured in seconds; timeout 参数为适当的 select() 或 poll() 调用设置超时参数,以秒为单位; the default is 30 seconds.默认值为 30 秒。

It's only firing the handle_write every 30 secs.它只是每 30 秒触发一次 handle_write。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM