import os, sys import threading, Queue import pyinotify from bazjunk.path import makedirs from bazbase import db, model, conversion MAX_THREADS = 5 queue = Queue.PriorityQueue() started = False def render_from_queue(): while True: neg_priority, ename, propname, format = queue.get() with db.begin_transaction(): e = model.Element.get(ename) rendering = conversion.render(e, propname, format) def start_render(ename, propnames, format, priority=0): global started if not started: # TODO(xavid): race started = True for x in xrange(MAX_THREADS): t = threading.Thread(target=render_from_queue) t.daemon = True t.start() for pn in propnames: queue.put((-priority, ename, pn, format)) def wait_for_render(ename, propnames, format): result_map = {} wm = pyinotify.WatchManager() dir = os.path.join(conversion.cache_dir(), ename) makedirs(dir) wdd = wm.add_watch(dir, pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO) notifier = pyinotify.Notifier(wm) while True: for pn in propnames: rendering = conversion.cached(ename, pn, format) if rendering is not None: result_map[pn] = rendering if result_map: return result_map # If nothing, wait for an event. notifier.check_events()