Sven Czarnian 3 лет назад
Родитель
Сommit
6b2072f43b
1 измененных файлов с 34 добавлено и 0 удалено
  1. 34 0
      aman/com/Euroscope.py

+ 34 - 0
aman/com/Euroscope.py

@@ -1,8 +1,10 @@
 #!/usr/bin/env python
 
+import ctypes
 import glob
 import os
 import sys
+import threading
 
 import zmq
 from zmq.asyncio import Context
@@ -11,6 +13,34 @@ from zmq.auth.asyncio import AsyncioAuthenticator
 
 from aman.config import Server
 
+class ReceiverThread(threading.Thread):
+    def __init__(self, socket):
+        threading.Thread.__init__(self)
+        self.socket = socket
+
+    def __del__(self):
+        self.stopThread()
+
+    def run(self):
+        try:
+            while True:
+                True
+        finally:
+            return
+
+    def threadId(self):
+        if hasattr(self, '_thread_id'):
+            return self._thread_id
+        for id, thread in threading._active.items():
+            if thread is self:
+                return id
+
+    def stopThread(self):
+        id = self.threadId()
+        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(id, ctypes.py_object(SystemExit))
+        if 1 < res:
+            ctypes.pythonapi.PyThreadState_SetAsyncExc(id, 0)
+
 # @brief Receives and sends messages to EuroScope plugins
 class Euroscope:
     # @brief Initializes the ZMQ socket
@@ -40,6 +70,8 @@ class Euroscope:
         self.receiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
         self.receiverSocket.setsockopt(zmq.CURVE_SERVER, True)
         self.receiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
+        self.receiverThread = ReceiverThread(self.receiverSocket)
+        self.receiverThread.start()
 
         # initialize the notification
         self.notificationSocket = self.context.socket(zmq.DEALER)
@@ -50,3 +82,5 @@ class Euroscope:
 
     def __exit__(self, *_exc):
         self.auth.stop()
+        self.receiverThread.stopThread()
+        self.receiverThread.join()