From 6b2072f43b8a815418b371cdf9488ec60b36fe04 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Sun, 15 Aug 2021 12:54:10 +0200 Subject: [PATCH] introduce a receiver thread --- aman/com/Euroscope.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/aman/com/Euroscope.py b/aman/com/Euroscope.py index 0e814fa..f55f5db 100644 --- a/aman/com/Euroscope.py +++ b/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()