From 68bfec4fe1b6567090515278e936fdde2fb8ef52 Mon Sep 17 00:00:00 2001
From: Martin Killenberg <martin.killenberg@desy.de>
Date: Tue, 7 Nov 2023 13:08:24 +0100
Subject: [PATCH] chore: move to spawn

---
 Python_script/MeasurementPlot.py        |  3 ++-
 Python_script/climate-lab-gui.py        | 18 ++++++++----------
 Python_script/process_sync.py           |  3 ++-
 Python_script/prototype.py              |  9 +++++----
 Python_script/shared_simulated_state.py |  8 ++++----
 5 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/Python_script/MeasurementPlot.py b/Python_script/MeasurementPlot.py
index ce3c796..a1db176 100644
--- a/Python_script/MeasurementPlot.py
+++ b/Python_script/MeasurementPlot.py
@@ -146,7 +146,8 @@ class MeasurementPlot:
 
         plt.rcParams.update({'font.size': 16})
 
-        self.data_queue = mp.Queue(10)
+        ctx = mp.get_context('spawn')
+        self.data_queue = ctx.Queue(10)
 
     def stop(self):
         self.data_queue.put((None, None, True))
diff --git a/Python_script/climate-lab-gui.py b/Python_script/climate-lab-gui.py
index 7ae0a07..b9f302b 100755
--- a/Python_script/climate-lab-gui.py
+++ b/Python_script/climate-lab-gui.py
@@ -107,14 +107,15 @@ class TestStandMainWindow(QMainWindow):
         elif self.measurementFileButton.isChecked():
             measurement_type = 'file'
 
-        error_queue = mp.Queue(3)
+        ctx = mp.get_context('spawn')
+        error_queue = ctx.Queue(3)
 
-        p = mp.Process(target=TestStandMainWindow.do_measurement_impl,
-                       args=(measurement_type, output_basename, self.startParameter.value(), self.stopParameter.value(),
-                             self.stepParameter.value(), self.fixedParameter.value(), self.soakingTime.value(),
-                             self.stableReads.value(), self.get_analysis_config(time_string),
-                             os.path.join(self.start_dir, self.measurementFile.text()), error_queue, config_data,
-                             ext_sensor_channels))
+        p = ctx.Process(target=TestStandMainWindow.do_measurement_impl,
+                        args=(measurement_type, output_basename, self.startParameter.value(), self.stopParameter.value(),
+                              self.stepParameter.value(), self.fixedParameter.value(), self.soakingTime.value(),
+                              self.stableReads.value(), self.get_analysis_config(time_string),
+                              os.path.join(self.start_dir, self.measurementFile.text()), error_queue, config_data,
+                              ext_sensor_channels))
 
         p.start()
         while p.is_alive():
@@ -181,9 +182,6 @@ class TestStandMainWindow(QMainWindow):
                 except FileNotFoundError as e:
                     error_queue.put(str(e))
 
-        except Exception as e:
-            print('ERROR: Exception during measurement: '+str(e))
-            error_queue.put(str(e))
 
         finally:
             if meas is not None:
diff --git a/Python_script/process_sync.py b/Python_script/process_sync.py
index 282d7c7..309c722 100644
--- a/Python_script/process_sync.py
+++ b/Python_script/process_sync.py
@@ -1,4 +1,5 @@
 import multiprocessing as mp
 
 # Global variables to be imported in multiple modules to avoid circular dependencies
-stop_measurement = mp.Event()
+ctx = mp.get_context('spawn')
+stop_measurement = ctx.Event()
diff --git a/Python_script/prototype.py b/Python_script/prototype.py
index 1ddd646..675cf36 100755
--- a/Python_script/prototype.py
+++ b/Python_script/prototype.py
@@ -13,7 +13,7 @@ import json
 import MeasurementPlot
 import sys
 import analysis
-from multiprocessing import Process
+import multiprocessing as mp
 import external_sensors
 import PostPlot
 import os
@@ -176,10 +176,11 @@ class Measurements:
         measurement sets of this chamber point are taken for this measurement.
         """
         process_sync.stop_measurement.clear()
-        measurement_process = Process(target=self.perform_single_measurement_impl,
-                                      args=(output, target_temp, target_hum, soaking_time, n_stable_reads))
+        ctx = mp.get_context('spawn')
+        measurement_process = ctx.Process(target=self.perform_single_measurement_impl,
+                                          args=(output, target_temp, target_hum, soaking_time, n_stable_reads))
         measurement_process.start()
-        #turn interactive plotting (back) on (is deactivated by starting the process)
+        # turn interactive plotting (back) on (is deactivated by starting the process)
         plt.ion()
         self.measurement_plot.drawing_loop()
         measurement_process.join()
diff --git a/Python_script/shared_simulated_state.py b/Python_script/shared_simulated_state.py
index 980ff70..c8872a2 100644
--- a/Python_script/shared_simulated_state.py
+++ b/Python_script/shared_simulated_state.py
@@ -12,7 +12,7 @@ def get_simulated_state():
 
 class SimulatedState:
     def __init__(self):
-
-        self.simulated_time = mp.Value('d', 0.0)
-        self.simulated_temperature = mp.Value('d', 25.)
-        self.simulated_humidity =  mp.Value('d', 35.)
+        ctx = mp.get_context('spawn')
+        self.simulated_time = ctx.Value('d', 0.0)
+        self.simulated_temperature = ctx.Value('d', 25.)
+        self.simulated_humidity = ctx.Value('d', 35.)
-- 
GitLab