Python : Running loop while waiting for server socket connection -
i have lcd use display timecode of movie watching on xbmc, , display current date , hour when nothing played. machine hosting lcd running python tcpsocketserver server receiving text displayed xbmc installation. problem have keep active network connection xbmc can send hour , date through socket , lcd can display it. in opinion, best thing display date of machine hosting lcd , switch "timecode view" when socket active.
is there "pythonic" way set tcpsocketserver compute loop displaying date while waiting connection, , change behaviour compute data received through socket ?
many in advance
here's code want. starts tcp server listening text sent port 9876. when gets text sends display object. code sets timer runs once second, sending "idle text" (ie: current timestamp) display object.
the display gets normal update text (from server), , "idle text" 2nd thread. object knows how long it's been since got real text, , displays 1 or other messages.
if display object attached hardware, use multiprocessing.rlock
or other mechanism protect itself.
have fun!
lcdcontrol.py source
import signal, socketserver, threading, time class display(object): idle_time = 5 # seconds def __init__(self): self.updated = none self.write('hello') def _write(self, arg): print 'display:', arg def write(self, arg): """ update display , 'last updated' timestamp """ self._write(arg) self.updated = time.time() def idle(self, arg): """ update display if it's been few seconds """ if time.time() - self.updated >= self.idle_time: self._write(arg) class displayhandler(socketserver.baserequesthandler): displayobj = none # class var def handle(self): text = self.request.recv(1024).strip() print "{} wrote: {}".format( self.client_address[0], text, ) # send text lcd self.displayobj.write(text) def check_idle(display_obj): """ update display current time if it's idle """ while true: display_obj.idle( time.strftime('time: %h:%m:%s'), ) time.sleep(1) def start_server(host, port): """ start (single threaded) server """ socketserver.tcpserver( (host, port), displayhandler, ).serve_forever() def main(host, port): display = display() # store global display obj handler can displayhandler.displayobj = display print 'serving on {}:{}'.format(host, port) print 'example: echo beer | nc localhost {}'.format(port) print server_t = threading.thread( target=start_server, args=(host, port) ) server_t.daemon = true server_t.start() idle_t = threading.thread( target=check_idle, args=[display], ) idle_t.daemon = true idle_t.start() # wait control-c interrupt try: signal.pause() except keyboardinterrupt: pass if __name__ == "__main__": main("localhost", 9876)
sample run
here started server, waited few seconds, typed fortune -s | nc localhost 9876
send short fortune cookie lcd server.
notice "idle timer" stopped @ :07
, output fortune cookie, waited 5 seconds, continued :13
, :14
. message displayed 5 seconds before switching idle timestamp.
python ./lcdcontrol.py display: hello serving on localhost:9876 example: echo beer | nc localhost 9876 display: time: 13:08:06 display: time: 13:08:07 127.0.0.1 wrote: people need imaginary cure painful imaginary ailment. display: people need imaginary cure painful imaginary ailment. display: time: 13:08:13 display: time: 13:08:14 display: time: 13:08:15 display: time: 13:08:16
Comments
Post a Comment