home *** CD-ROM | disk | FTP | other *** search
- #
- # This file is part of OpenVIP (http://openvip.sourceforge.net)
- #
- # Copyright (C) 2002-2003
- # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
- #
- # This program is licensed under GNU General Public License version 2;
- # see file COPYING in the top level directory for details.
- #
- # $Id: worker.py,v 1.7 2003/06/03 11:00:37 vaclavslavik Exp $
- #
- # Simple worker thread interface. There are two threads in OpenVIP GUI:
- # the main thread (running wxPython event loop) and worked thread
- # (rendering using OpenVIP core).
- #
-
- import threading
-
-
- stopped = False
- running = False
- queue = []
- cv = threading.Condition()
-
- class Thread(threading.Thread):
- def run(self):
- global stopped, running
- while not stopped:
- cv.acquire()
- while len(queue) == 0:
- if stopped: break
- cv.wait()
- if stopped:
- cv.release()
- break
- func,arg = queue.pop(0)
- cv.release()
- if arg == None: func()
- else: func(arg)
- running = False
-
- thread = None
-
- def enqueue(func, arg=None):
- """Adds a function to scheduled tasks queue."""
- global thread, running
- if not running:
- thread = Thread()
- thread.start()
- running = True
- if stopped:
- return
- cv.acquire()
- queue.append((func,arg))
- cv.notify()
- cv.release()
-
- def stop():
- """Stops processing -- waits till the queue is empty."""
- if not running: return
- import time
- global stopped
-
- cv.acquire()
- stopped = True
- cv.notify()
- cv.release()
- thread.join()
-