My code accepts gps data from multiple sources aggregates it and sends it back out to multiple clients connected to a threaded single socket. I got it working but the output threads seem to run off with cpu resources.
if I add code to wait for some data from the client the cpu use disappears but the clients only accept a stream of gps info they don't send anything.
Below is the server code that sends data ok but runs high 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()
if I change it to below cpu stops but it only outputs data if I send a keystroke.
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
Is there any way to pause the thread until data is sent? or can I simulate a received message to trigger a data burst from the main program?