Do not wait for update lock, exit

This commit is contained in:
ZyX 2013-03-24 18:45:20 +04:00
parent 63a50ad200
commit ca2f0cc873
1 changed files with 16 additions and 8 deletions

View File

@ -23,9 +23,9 @@ class ThreadedSegment(object):
self.did_set_interval = False self.did_set_interval = False
self.thread = None self.thread = None
def __call__(self, update_first=True, **kwargs): def __call__(self, pl, update_first=True, **kwargs):
if self.run_once: if self.run_once:
self.pl = kwargs['pl'] self.pl = pl
self.set_state(**kwargs) self.set_state(**kwargs)
self.update() self.update()
elif not self.is_alive(): elif not self.is_alive():
@ -39,7 +39,7 @@ class ThreadedSegment(object):
self.start() self.start()
with self.write_lock: with self.write_lock:
return self.render(update_first=update_first, **kwargs) return self.render(update_first=update_first, pl=pl, **kwargs)
def is_alive(self): def is_alive(self):
return self.thread and self.thread.is_alive() return self.thread and self.thread.is_alive()
@ -56,11 +56,19 @@ class ThreadedSegment(object):
while self.keep_going: while self.keep_going:
start_time = monotonic() start_time = monotonic()
with self.update_lock: try:
try: if self.update_lock.acquire(False):
self.update() try:
except Exception as e: self.update()
self.error('Exception while updating: {0}', str(e)) except Exception as e:
self.error('Exception while updating: {0}', str(e))
else:
return
finally:
# Release lock in any case. If it is not locked in this thread,
# it was done in main thread in .shutdown method, and the lock
# will never be released.
self.update_lock.release()
self.sleep(monotonic() - start_time) self.sleep(monotonic() - start_time)