issues with Trellis event loops and threads
"Sergey Schetinin" <[email protected]>
| Newsgroups | gmane.comp.python.peak |
|---|---|
| Message-ID | <[email protected]> |
There's a problem with using the trellis and activity module in a multithreaded app. There's a main loop thread with activity.EventLoop running and there's a rule that runs periodically by rescheduling itself (in my case it calls wx.ProgressBar.Pulse(), just to give you an example of why this could be a legitimate use). There's also a worker thread that initiates this process, but due to EventLoop and Time being context.Service this doesn't work as expected -- the dependency on _Timer instance (Time[0.1] for example) created from a worker thread doesn't schedule a recalculation. See test in attached file -- it should't hang up, but it does as do_update rule only runs once. I tried to fix it by restoring state from the main thread in the worker thread but it's rather non-trivial as the State instances seem to rely on a threadlocal data. If there's a proper way to do it, I'd be happy to learn what it is. Thanks a lot in advance. -- Best Regards, Sergey Schetinin http://s3bk.com/ -- S3 Backup http://word-to-html.com/ -- Word to HTML Converter _______________________________________________ PEAK mailing list [email protected] http://www.eby-sarna.com/mailman/listinfo/peak
test_threads.py
(text/x-python, 712 B)
from peak.events.trellis import *
from peak.events.activity import *
from threading import Thread
class ThreadedComponent(Component):
updating = attr(False)
iteration = attr(0)
time_to_update = attr(EPOCH)
@maintain
def do_update(self):
if self.updating and self.time_to_update:
#print 'UPDATING'
self.iteration += 1
if self.iteration == 2:
EventLoop.stop()
self.time_to_update = Time[0.1]
bool(self.time_to_update) # make sure we depend on new value
c = ThreadedComponent()
def start_updating_in_thread():
c.updating = True
th = Thread(target=start_updating_in_thread)
th.start()
EventLoop.run()