Python socketserver向多个客户端发送数据cpu使用率高
我的代码从多个来源接收gps数据,并将其聚合后发送回连接到单个线程套接字的多个客户端。我让它工作,但输出线程似乎运行了cpu资源 如果我添加代码等待来自客户端的一些数据,cpu使用就会消失,但是客户端只接受gps信息流,而不发送任何内容 下面是发送数据正常但运行高CPU的服务器代码Python socketserver向多个客户端发送数据cpu使用率高,python,multithreading,cpu-usage,socketserver,Python,Multithreading,Cpu Usage,Socketserver,我的代码从多个来源接收gps数据,并将其聚合后发送回连接到单个线程套接字的多个客户端。我让它工作,但输出线程似乎运行了cpu资源 如果我添加代码等待来自客户端的一些数据,cpu使用就会消失,但是客户端只接受gps信息流,而不发送任何内容 下面是发送数据正常但运行高CPU的服务器代码 class ThreadedServerRequestHandler(SocketServer.StreamRequestHandler): def handle(self): global
class ThreadedServerRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
global SendData
global SendNow
while True:
SendNow
for line in SendData:
self.request.sendall(line)
SendData = []
SendNow = False
return
class ServerThread(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
daemon_threads = True
allow_reuse_address = True
if __name__ == '__main__':
import socket
import threading
address = TxServer
server = ServerThread(address, ThreadedServerRequestHandler)
t = threading.Thread(target=server.serve_forever)
t.setDaemon(True) # don't hang on exit
t.start()
如果我将其更改为低于cpu停止,但它仅在我发送击键时输出数据
class ThreadedServerRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
global SendData
global SendNow
while True:
self.data = self.request.recv(1024).strip()
if self.data == '':
print 'closing thread'
break
while SendNow == True:
for line in SendData:
self.request.sendall(line)
SendData = []
SendNow = False
return
有没有办法暂停线程直到发送数据?或者我可以模拟接收到的消息来触发来自主程序的数据突发吗?它使用100%CPU的原因是,当您没有什么要写的时候,您只需尽可能快地旋转,直到有东西要写:
while True:
SendNow
for line in SendData:
self.request.sendall(line)
SendData = []
SendNow = False
要使它不使用100%的CPU,您必须找到一些东西让它等待
您的修复程序通过等待接收到的数据来实现这一点,但由于您通常没有任何数据可接收,因此这不是很有用。(正如您所说,“它只在我发送击键时输出数据”。)
同时:
有没有办法暂停线程直到发送数据
当然。你已经在做了。这就是sendall
所做的。但那没用。问题是一旦你发送了所有的数据,你就会一遍又一遍地重复循环,直到有更多的数据要发送
或者我可以模拟接收到的消息来触发来自主程序的数据突发吗
当然,但是你会用什么来触发模拟接收呢?如果你只是想尽可能快地旋转,那不会有任何帮助
我想你想要的是一个关于数据的讨论。大概是这样的:
SendCondition = threading.Condition()
class ThreadedServerRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
global SendCondition
global SendData
while True:
with SendCondition:
while not SendData:
SendCondition.wait()
for line in SendData:
self.request.sendall(line)
SendData = []
global SendCondition
global SendData
# ...
new_send_data = <whatever>
with SendCondition:
SendData.append(new_send_data)
SendCondition.notify()
然后,设置SendData
(未显示)的代码如下所示:
SendCondition = threading.Condition()
class ThreadedServerRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
global SendCondition
global SendData
while True:
with SendCondition:
while not SendData:
SendCondition.wait()
for line in SendData:
self.request.sendall(line)
SendData = []
global SendCondition
global SendData
# ...
new_send_data = <whatever>
with SendCondition:
SendData.append(new_send_data)
SendCondition.notify()
全局发送条件
全局发送数据
# ...
新发送数据=
在发送条件下:
追加(新的发送数据)
SendCondition.notify()
Brilliant,工作起来很有魅力。这就是谷歌编程的问题所在,我不知道我不知道什么,所以我的搜索问题受限于我对allready的理解。@user2304910:这就是为什么值得学习教程,而不仅仅是获取示例代码并试图找出答案的原因。问题是,通常没有任何好的教程来指导你要做的事情。而且,如果有,很难找到。(我肯定有人写了一篇很棒的“介绍Python中线程之间安全共享数据的不同方法”,但我不知道在哪里可以找到它……)