def run_sample_app(): import SocketServer SocketServer.BaseServer.handle_error = close_stream import example_app example_app.run(app, xyzzy)
def pop_queue(current_event_queue): while True: data = current_event_queue.get() yield data def purge_streams(): big_queues = [ port for port, queue in xyzzy.events_queue if len(queue) > xyzzy.MAX_QUEUE_LENGTH ] for big_queue in big_queues: current_app.logger.info( 'Client %s is stale. Disconnecting. Total Clients: %s' % (big_queue, len(xyzzy.events_queue))) del queue[big_queue] def close_stream(*args, **kwargs): event_stream_port = args[2][1] del xyzzy.events_queue[event_stream_port] print('Client %s disconnected. Total Clients: %s' % (event_stream_port, len(xyzzy.events_queue))) if __name__ == "__main__": import SocketServer SocketServer.BaseServer.handle_error = close_stream import example_app example_app.run(app, xyzzy)
import example_app print("Let's explore some Handy Features of Python ") example_app.run()
return Response(pop_queue(current_event_queue), mimetype='text/event-stream') return Response(xyzzy.last_events.values(), mimetype='text/event-stream') def pop_queue(current_event_queue): while not xyzzy.stopped: try: data = current_event_queue.get(timeout=0.1) yield data except Queue.Empty: #this makes the server quit nicely - previously the queue threads would block and never exit. This makes it keep checking for dead application pass def purge_streams(): big_queues = [port for port, queue in xyzzy.events_queue if len(queue) > xyzzy.MAX_QUEUE_LENGTH] for big_queue in big_queues: current_app.logger.info('Client %s is stale. Disconnecting. Total Clients: %s' % (big_queue, len(xyzzy.events_queue))) del queue[big_queue] def close_stream(*args, **kwargs): event_stream_port = args[2][1] del xyzzy.events_queue[event_stream_port] log.info('Client %s disconnected. Total Clients: %s' % (event_stream_port, len(xyzzy.events_queue))) if __name__ == "__main__": import SocketServer SocketServer.BaseServer.handle_error = close_stream import example_app example_app.run(app, xyzzy)