| 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 |
import time |
| 27 |
import traceback |
| 28 |
from queue import Queue |
| 29 |
|
| 30 |
from .listeners import Recorder |
| 31 |
from .procedure import Procedure |
| 32 |
from .results import Results |
| 33 |
from ..thread import StoppableThread |
| 34 |
|
| 35 |
log = logging.getLogger(__name__) |
| 36 |
log.addHandler(logging.NullHandler()) |
| 37 |
|
| 38 |
try: |
| 39 |
import zmq |
| 40 |
import cloudpickle |
| 41 |
except ImportError: |
| 42 |
zmq = None |
| 43 |
cloudpickle = None |
| 44 |
log.warning("ZMQ and cloudpickle are required for TCP communication") |
| 45 |
|
| 46 |
|
| 47 |
class Worker(StoppableThread): |
| 48 |
""" Worker runs the procedure and emits information about |
| 49 |
the procedure and its status over a ZMQ TCP port. In a child |
| 50 |
thread, a Recorder is run to write the results to |
| 51 |
""" |
| 52 |
|
| 53 |
def __init__(self, results, log_queue=None, log_level=logging.INFO, port=None): |
| 54 |
""" Constructs a Worker to perform the Procedure |
| 55 |
defined in the file at the filepath |
| 56 |
""" |
| 57 |
super().__init__() |
| 58 |
|
| 59 |
self.port = port |
| 60 |
if not isinstance(results, Results): |
| 61 |
raise ValueError("Invalid Results object during Worker construction") |
| 62 |
self.results = results |
| 63 |
self.results.procedure.check_parameters() |
| 64 |
self.results.procedure.status = Procedure.QUEUED |
| 65 |
|
| 66 |
self.recorder = None |
| 67 |
self.recorder_queue = Queue() |
| 68 |
|
| 69 |
self.monitor_queue = Queue() |
| 70 |
if log_queue is None: |
| 71 |
log_queue = Queue() |
| 72 |
self.log_queue = log_queue |
| 73 |
self.log_level = log_level |
| 74 |
|
| 75 |
global log |
| 76 |
log = logging.getLogger() |
| 77 |
log.setLevel(self.log_level) |
| 78 |
# log.handlers = [] # Remove all other handlers |
| 79 |
# log.addHandler(TopicQueueHandler(self.monitor_queue)) |
| 80 |
# log.addHandler(QueueHandler(self.log_queue)) |
| 81 |
|
| 82 |
self.context = None |
| 83 |
self.publisher = None |
| 84 |
if self.port is not None and zmq is not None: |
| 85 |
try: |
| 86 |
self.context = zmq.Context() |
| 87 |
log.debug("Worker ZMQ Context: %r" % self.context) |
| 88 |
self.publisher = self.context.socket(zmq.PUB) |
| 89 |
self.publisher.bind('tcp://*:%d' % self.port) |
| 90 |
log.info("Worker connected to tcp://*:%d" % self.port) |
| 91 |
# wait so that the socket will be ready before starting to emit messages |
| 92 |
time.sleep(0.3) |
| 93 |
except Exception: |
| 94 |
log.exception("Couldn't establish ZMQ publisher!") |
| 95 |
self.context = None |
| 96 |
self.publisher = None |
| 97 |
|
| 98 |
def join(self, timeout=0): |
| 99 |
try: |
| 100 |
super().join(timeout) |
| 101 |
except (KeyboardInterrupt, SystemExit): |
| 102 |
log.warning("User stopped Worker join prematurely") |
| 103 |
self.stop() |
| 104 |
super().join(0) |
| 105 |
|
| 106 |
def emit(self, topic, record): |
| 107 |
""" Emits data of some topic over TCP """ |
| 108 |
log.debug("Emitting message: %s %s", topic, record) |
| 109 |
|
| 110 |
try: |
| 111 |
self.publisher.send_serialized( |
| 112 |
record, |
| 113 |
serialize=lambda rec: (topic.encode(), cloudpickle.dumps(rec)), |
| 114 |
) |
| 115 |
except (NameError, AttributeError): |
| 116 |
pass # No dumps defined |
| 117 |
if topic == 'results': |
| 118 |
self.recorder.handle(record) |
| 119 |
elif topic == 'status' or topic == 'progress': |
| 120 |
self.monitor_queue.put((topic, record)) |
| 121 |
|
| 122 |
def handle_abort(self): |
| 123 |
log.exception("User stopped Worker execution prematurely") |
| 124 |
self.update_status(Procedure.ABORTED) |
| 125 |
|
| 126 |
def handle_error(self): |
| 127 |
log.exception("Worker caught an error on %r", self.procedure) |
| 128 |
traceback_str = traceback.format_exc() |
| 129 |
self.emit('error', traceback_str) |
| 130 |
self.update_status(Procedure.FAILED) |
| 131 |
|
| 132 |
def update_status(self, status): |
| 133 |
self.procedure.status = status |
| 134 |
self.emit('status', status) |
| 135 |
|
| 136 |
def shutdown(self): |
| 137 |
self.procedure.shutdown() |
| 138 |
|
| 139 |
if self.should_stop() and self.procedure.status == Procedure.RUNNING: |
| 140 |
self.update_status(Procedure.ABORTED) |
| 141 |
elif self.procedure.status == Procedure.RUNNING: |
| 142 |
self.update_status(Procedure.FINISHED) |
| 143 |
self.emit('progress', 100.) |
| 144 |
|
| 145 |
self.recorder.stop() |
| 146 |
self.monitor_queue.put(None) |
| 147 |
if self.context is not None: |
| 148 |
# Cleanly close down ZMQ context and associated socket |
| 149 |
# For some reason, we need to close the socket before the |
| 150 |
# context, otherwise context termination hangs. |
| 151 |
self.publisher.close() |
| 152 |
self.context.term() |
| 153 |
|
| 154 |
def run(self): |
| 155 |
log.info("Worker thread started") |
| 156 |
|
| 157 |
self.procedure = self.results.procedure |
| 158 |
|
| 159 |
self.recorder = Recorder(self.results, self.recorder_queue) |
| 160 |
self.recorder.start() |
| 161 |
|
| 162 |
# locals()[self.procedures_file] = __import__(self.procedures_file) |
| 163 |
|
| 164 |
# route Procedure methods & log |
| 165 |
self.procedure.should_stop = self.should_stop |
| 166 |
self.procedure.emit = self.emit |
| 167 |
|
| 168 |
log.info("Worker started running an instance of %r", self.procedure.__class__.__name__) |
| 169 |
self.update_status(Procedure.RUNNING) |
| 170 |
self.emit('progress', 0.) |
| 171 |
|
| 172 |
try: |
| 173 |
self.procedure.startup() |
| 174 |
self.procedure.evaluate_metadata() |
| 175 |
self.results.store_metadata() |
| 176 |
self.procedure.execute() |
| 177 |
except (KeyboardInterrupt, SystemExit): |
| 178 |
self.handle_abort() |
| 179 |
except Exception: |
| 180 |
self.handle_error() |
| 181 |
finally: |
| 182 |
self.shutdown() |
| 183 |
self.stop() |
| 184 |
|
| 185 |
def __repr__(self): |
| 186 |
return "<{}(port={},procedure={},should_stop={})>".format( |
| 187 |
self.__class__.__name__, self.port, |
| 188 |
self.procedure.__class__.__name__, |
| 189 |
self.should_stop() |
| 190 |
) |