diff --git a/billboard/app.py b/billboard/app.py index d3a5607..c365914 100644 --- a/billboard/app.py +++ b/billboard/app.py @@ -4,7 +4,7 @@ import signal import tempfile import logging - +import queue from PyQt4.QtGui import QApplication @@ -47,11 +47,14 @@ def main(): logging.debug('Creating {}'.format(workdir)) os.makedirs(workdir) - server = Server(workdir, args.port) + # We use a queue to communicate between threads + q = queue.Queue() + + server = Server(workdir, args.port, q) display = BillboardDisplay(workdir=workdir) sources = [RedditSource()] - billboard = Billboard(display, sources, args.period) + billboard = Billboard(display, sources, args.period, q) billboard.start() display.showFullScreen() diff --git a/billboard/billboard.py b/billboard/billboard.py index 7524ef3..9e18b76 100644 --- a/billboard/billboard.py +++ b/billboard/billboard.py @@ -1,15 +1,17 @@ import time from threading import Thread from itertools import cycle +from queue import Empty class Billboard(Thread): - def __init__(self, display, sources, period): + def __init__(self, display, sources, period, q): super(Billboard, self).__init__() self.display = display self.sources = sources self.period = period + self.q = q self.daemon = True def run(self): @@ -21,5 +23,13 @@ def run(self): self.display.update_image(image) if text is not None: self.display.display_text(text) + # HACK: We sleep for a moment to make sure that this + # display has updated before we take the screenshot. This + # is not an issue on slower devices, but can occur on + # faster machines. + time.sleep(1) self.display.update_current() - time.sleep(self.period) + try: + self.q.get(timeout=self.period) + except Empty: + pass diff --git a/billboard/server.py b/billboard/server.py index c03dd86..dbad463 100644 --- a/billboard/server.py +++ b/billboard/server.py @@ -1,16 +1,17 @@ import os import threading -from flask import Flask,request, send_from_directory +from flask import Flask, request, send_from_directory class Server(threading.Thread): - def __init__(self, workdir, port): + def __init__(self, workdir, port, q): super(Server, self).__init__() self.daemon = True self.workdir = workdir self.port = port + self.q = q def run(self): app = Flask('billboard') @@ -26,6 +27,14 @@ def current(): response.headers['Pragma'] = 'no-cache' return response + @app.route('/next') + def next(): + # We just put SOMETHING into the queue. It doesn't matter + # what it is as we discard it anyhow. + self.q.put(None) + return "Loading the next image!" + + app.run(host='0.0.0.0', port=self.port)