[weboob] Threads in weboob
romain at peerfuse.org
Tue Apr 27 01:29:45 CEST 2010
On 26/Apr - 19:50, Christophe Benz wrote:
> I agree with this idea.
> I have no time to answer in details, but let's do the job!
Right, I started to work on it. It works fine with the videoob frontend.
The API is:
for backend, result in self.weboob.do('iter_search_results', pattern):
Weboob.do creates a BackendsCall object, which starts a thread for every
backend. The __iter__ method waits for new responses, and yield a Result object
each time there is a response.
The Result object has an __iter__ attribute, it is usefull to do:
backend, result = self.weboob.do(...)
There is also a Weboob.do_caps() method to do operations only on backends with
I also created a method 'callback_thread' method in BackendsCall. You can use it
if you want to have a function called in a thread each time a new results
arrives. For example, in a Qt application:
def cb(backend, result):
Qt.emit(Qt.SIGNAL('new_result', backend, result))
c = self.weboob.do('iter_search_results', pattern)
The 'thread' in method is important to keep in mind that the callback will be
called in a separated thread.
Another thing, now Backend and Browser classes are protected with a mutex which
can be locked if you want. Note that if you want to use them safely, it is
recommanded to use the 'with' statement:
The backend's mutex is locked when entering in the 'with' bloc, and released
when leaving it, even if an exception is raised.
Now, a big problem is about exceptions when you call Weboob.do. A backend's
method can raise legitimate exception to be catched on frontend.
But with this new API, there is no way to catch a global backend exception,
because the frontend get results regardless of backends order.
(Note: I'm tired)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 489 bytes
Desc: not available
More information about the weboob