Python socketserver向多个客户端发送数据cpu使用率高

标签 python multithreading cpu-usage socketserver

我的代码接受来自多个源的 GPS 数据,将其聚合并将其发送回连接到单个线程套接字的多个客户端。我让它工作了,但输出线程似乎耗尽了 CPU 资源。

如果我添加代码来等待来自客户端的某些数据,CPU 使用就会消失,但客户端只接受 GPS 信息流,它们不会发送任何内容。

下面是发送数据正常但 CPU 运行较高的服务器代码

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,你必须找到一些东西让它等待。

您的修复通过等待接收到的数据来实现此目的,但由于您通常没有任何数据要接收,因此这不是很有用。 (正如您所说,“如果我发送击键,它只会输出数据”。)

同时:

Is there any way to pause the thread until data is sent?

当然。而你已经在这么做了。这就是 sendall 的作用。但这没有帮助。问题是,一旦您发送了所有数据,您就会一遍又一遍地返回循环,直到有更多数据要发送为止。

or can I simulate a received message to trigger a data burst from the main program?

当然,但是您将使用什么来触发模拟接收?如果您只是想尽可能快地旋转模拟接收,那将没有任何帮助。

我想你想要的是 condition variable围绕数据。像这样的事情:

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 = []

然后,无论你的代码是什么,设置 SendData (你没有显示)看起来像这样:

global SendCondition
global SendData
# ...
new_send_data = <whatever>
with SendCondition:
    SendData.append(new_send_data)
    SendCondition.notify()

关于Python socketserver向多个客户端发送数据cpu使用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16180636/

相关文章:

python - python中的循环缩进

ios - GCD 错误 :Tried to obtain the web lock from a thread other than the main thread or the web thread

c++ - 如何在 C++ 中模拟用户空间的线程本地存储?

java - 如何使用 Java 11 获取 CPU 利用率

python - 奇怪的日期格式为 YY-mm-dd 中的部分列和 YY-dd-mm 中的其余列

python - 如何更改游戏之间的播放音乐

Python 使用 id() 在内存中传递对象引用

c# - 运行时不同线程中变量的作用域

Windows 10 上的 Python 多处理

c++ - 如何在 Windows 上限制进程的 CPU 使用率?