[WebInterface] - forward opener for externals to have access to functions of WebInterface
[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         instances = []
7
8         def __init__(self, fnc):
9                 threading.Thread.__init__(self)
10                 self.deferred = defer.Deferred()
11                 self.__pump = ePythonMessagePump()
12                 self.__pump_conn = self.__pump.recv_msg.connect(self.gotThreadMsg)
13                 self.__asyncFunc = fnc
14                 self.__result = None
15                 self.__err = None
16                 SimpleThread.instances.append(self)
17
18         def gotThreadMsg(self, msg):
19                 if self.__err:
20                         self.deferred.errback(self.__err)
21                 else:
22                         self.deferred.callback(self.__result)
23                 SimpleThread.instances.remove(self)
24                 del self.__pump_conn
25
26         def run(self):
27                 try:
28                         self.__result = self.__asyncFunc()
29                 except Exception as e:
30                         self.__err = e
31                 finally:
32                         self.__pump.send(0)
33
34 __all__ = ['SimpleThread']