0

以下课程有效,服务器运行良好,但我正在最大限度地使用 cpu,所以我认为在我的课程构造中我没有看到明显的错误。

我仍然是一个新手 Python 脚本编写者,因此在优化我的代码方面有任何有用的反馈将不胜感激。

# -*- coding: utf-8 -*-
#!/usr/bin/env python3

import sys
import types
from time import sleep

from OSC import OSCServer
import pifacedigitalio as pf



class App:
    def __init__(self,ip,port):
        self._ip=ip
        self._port=port
        self.r1 = pf.Relay(0)
        self.r2 = pf.Relay(1)
        pf.init()
        self.server = OSCServer( ( self._ip, self._port) )
        self.server.addMsgHandler( "/1/push1", self.user_callback )
        self.server.addMsgHandler( "/1/push2", self.user_callback )
        self.server.addMsgHandler( "/quit", self.quit_callback )
        self.server.timeout = 0
        self.server.handle_timeout = types.MethodType(self.handle_timeout, self.server)

        self.run = True
        print ("YES Server is running")
        while self.run:
            sleep(0.01)
            self.each_frame()  
            self.server.close()



    def user_callback(self,path, tags, args, source):
        # callback code here


    def quit_callback(self,path, tags, args, source):
        self.run = False

    def handle_timeout(self,server):
        self.timed_out = True 

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            self.server.handle_request()       


def main():
    app = App("0.0.0.0",9000)

if __name__ == "__main__": main() 
4

1 回答 1

0
def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            self.server.handle_request() 

def each_frame(self):
        self.timed_out = False
        while not self.timed_out:
            self.server.handle_request()           
于 2013-11-07T14:44:49.307 回答