ocarina/libsaria/event.py
Bryan Schumaker 15cc5d22f0 Backend and frontend changes
Libsaria events now support starting specific callback functions in a
background thread.  This replaces starting a specific event in the
background.

I have a library tab that is added to the main window through use of the
ocarina.add_tab function.

I have new tests for walking the tree and running multiple threads with
locks.
2010-08-19 23:02:30 -04:00

65 lines
1.1 KiB
Python

# Bryan Schumaker (8/7/2010)
events = dict()
threads = None
start_thread = None
def invite(key, func, bg=False):
global events
if key not in events:
events[key] = set()
events[key].add((func,bg))
def uninvite(key, func):
global events
if key not in events:
return
events[key].discard(func)
def start_thread_once(func, *args):
global threads
global start_thread
import threads
start_thread = threads.background
return threads.background(func, *args)
start_thread = start_thread_once
def start(key, *args):
global events
if key not in events:
return
for func, bg in events[key]:
try:
if bg == False:
func(*args)
else:
start_thread(func, *args)
except Exception,e:
print
print "===== EVENT ERROR ====="
print "Event:", key
print "Function:", func
print "Args:", args
print e
print "The function has been uninvited from this event"
print "====="
def call(action, func=None, *args):
start("PRE%s" % action, *args)
if func == None:
return None
res = func(*args)
start("POST%s" % action, res)
return res