- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要连接到服务器(例如 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()
这是运行时的输出:
$ 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 秒通过缓冲区变量向服务器发送数据,但 asyncore 恰好每 1 分钟调用一次 writable 和 handle_write,我不明白为什么它在从线程填充缓冲区后不获取缓冲区?
最佳答案
查看 asyncore loop 的文档方法。
The timeout argument sets the timeout parameter for the appropriate select() or poll() call, measured in seconds; the default is 30 seconds.
它只会每 30 秒触发一次 handle_write。
关于Python 的 asyncore 客户端向服务器发送周期性数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7056973/
我已经完成了创建和启动计时器的手册页。 http://man7.org/linux/man-pages/man2/timerfd_create.2.html 但是,除了 arm(start) 和 di
我正在用 opengl 编写新的代码库,很早就遇到了一个奇怪的错误。这是帧速率的明显波动,具有重复性和可预测性。 我知道它肯定与渲染的对象成正比。它也与屏幕大小成正比(不是视口(viewport)大小
我知道如何使用计算数组中点之间的欧几里得距离 scipy.spatial.distance.cdist 类似于这个问题的答案: Calculate Distances Between One Poin
我想使用 CGAL 构造周期性 3D Delaunay 三角剖分和信息(在本例中为整数)。对于 2D,如果我构造一个 vector 对(点,信息)并将其传递给三角测量函数,则效果很好。然而,非常类似的
每隔几天,我们就会收到少量 MySql 超时错误,这些错误与我们的 MySQL RDS 实例上的 CPU 和数据库连接出现大量峰值相对应。这些查询通常非常快(<5 毫秒),但突然超时。 此时,数据库操
我是一名优秀的程序员,十分优秀!