| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
|
| 27 |
from .Qt import QtCore |
| 28 |
from .thread import StoppableQThread |
| 29 |
from ..experiment.procedure import Procedure |
| 30 |
|
| 31 |
log = logging.getLogger(__name__) |
| 32 |
log.addHandler(logging.NullHandler()) |
| 33 |
|
| 34 |
try: |
| 35 |
import zmq |
| 36 |
import cloudpickle |
| 37 |
except ImportError: |
| 38 |
zmq = None |
| 39 |
cloudpickle = None |
| 40 |
log.warning("ZMQ and cloudpickle are required for TCP communication") |
| 41 |
|
| 42 |
|
| 43 |
class QListener(StoppableQThread): |
| 44 |
"""Base class for QThreads that need to listen for messages |
| 45 |
on a ZMQ TCP port and can be stopped by a thread- and process-safe |
| 46 |
method call |
| 47 |
""" |
| 48 |
|
| 49 |
def __init__(self, port, topic='', timeout=0.01): |
| 50 |
""" Constructs the Listener object with a subscriber port |
| 51 |
over which to listen for messages |
| 52 |
|
| 53 |
:param port: TCP port to listen on |
| 54 |
:param topic: Topic to listen on |
| 55 |
:param timeout: Timeout in seconds to recheck stop flag |
| 56 |
""" |
| 57 |
super().__init__() |
| 58 |
|
| 59 |
self.port = port |
| 60 |
self.topic = topic |
| 61 |
self.context = zmq.Context() |
| 62 |
log.debug(f"{self.__class__.__name__} has ZMQ Context: {self.context!r}") |
| 63 |
self.subscriber = self.context.socket(zmq.SUB) |
| 64 |
self.subscriber.connect('tcp://localhost:%d' % port) |
| 65 |
self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode()) |
| 66 |
log.info("%s connected to '%s' topic on tcp://localhost:%d" % ( |
| 67 |
self.__class__.__name__, topic, port)) |
| 68 |
|
| 69 |
self.poller = zmq.Poller() |
| 70 |
self.poller.register(self.subscriber, zmq.POLLIN) |
| 71 |
self.timeout = timeout |
| 72 |
|
| 73 |
def receive(self, flags=0): |
| 74 |
topic, record = self.subscriber.recv_serialized( |
| 75 |
deserialize=lambda msg: (msg[0].decode(), cloudpickle.loads(msg[1])), |
| 76 |
flags=flags |
| 77 |
) |
| 78 |
return topic, record |
| 79 |
|
| 80 |
def message_waiting(self): |
| 81 |
return self.poller.poll(self.timeout) |
| 82 |
|
| 83 |
def __repr__(self): |
| 84 |
return "<{}(port={},topic={},should_stop={})>".format( |
| 85 |
self.__class__.__name__, self.port, self.topic, self.should_stop()) |
| 86 |
|
| 87 |
|
| 88 |
class Monitor(QtCore.QThread): |
| 89 |
""" Monitor listens for status and progress messages |
| 90 |
from a Worker through a queue to ensure no messages |
| 91 |
are losts |
| 92 |
""" |
| 93 |
|
| 94 |
status = QtCore.Signal(int) |
| 95 |
progress = QtCore.Signal(float) |
| 96 |
log = QtCore.Signal(object) |
| 97 |
worker_running = QtCore.Signal() |
| 98 |
worker_failed = QtCore.Signal() |
| 99 |
worker_finished = QtCore.Signal() # Distinguished from QThread.finished |
| 100 |
worker_abort_returned = QtCore.Signal() |
| 101 |
|
| 102 |
def __init__(self, queue): |
| 103 |
super().__init__() |
| 104 |
self.queue = queue |
| 105 |
|
| 106 |
def run(self): |
| 107 |
while True: |
| 108 |
data = self.queue.get() |
| 109 |
if data is None: |
| 110 |
break |
| 111 |
topic, data = data |
| 112 |
if topic == 'status': |
| 113 |
self.status.emit(data) |
| 114 |
if data == Procedure.RUNNING: |
| 115 |
self.worker_running.emit() |
| 116 |
elif data == Procedure.FAILED: |
| 117 |
self.worker_failed.emit() |
| 118 |
elif data == Procedure.FINISHED: |
| 119 |
self.worker_finished.emit() |
| 120 |
elif data == Procedure.ABORTED: |
| 121 |
self.worker_abort_returned.emit() |
| 122 |
elif topic == 'progress': |
| 123 |
self.progress.emit(data) |
| 124 |
elif topic == 'log': |
| 125 |
self.log.emit(data) |
| 126 |
|
| 127 |
log.info("Monitor caught stop command") |