Merge branch 'master' into master_internal
[enigma2-plugins.git] / toolkit / src / SimpleThread.py
1 from enigma import ePythonMessagePump
2 import threading
3 from twisted.internet import defer
4
5 class SimpleThread(threading.Thread):
6         def __init__(self, fnc):
7                 threading.Thread.__init__(self)
8                 self.deferred = defer.Deferred()
9                 self.__pump = ePythonMessagePump()
10                 self.__pump_conn = self.__pump.recv_msg.connect(self.gotThreadMsg)
11                 self.__asyncFunc = fnc
12                 self.__result = None
13                 self.__err = None
14
15         def gotThreadMsg(self, msg):
16                 if self.__err:
17                         self.deferred.errback(self.__err)
18                 else:
19                         self.deferred.callback(self.__result)
20                 del self.__pump_conn
21
22         def run(self):
23                 try:
24                         self.__result = self.__asyncFunc()
25                 except Exception as e:
26                         self.__err = e
27                 finally:
28                         self.__pump.send(0)
29
30 __all__ = ['SimpleThread']