From cd852ee98d436fe032392e1e7f810fa55f8ad8d2 Mon Sep 17 00:00:00 2001
From: Martin Killenberg <martin.killenberg@desy.de>
Date: Thu, 2 Nov 2023 22:32:07 +0100
Subject: [PATCH] feat: use multiprocess instead of threads

---
 Python_script/MeasurementPlot.py |  6 +++++-
 Python_script/prototype.py       | 17 ++++++++++-------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/Python_script/MeasurementPlot.py b/Python_script/MeasurementPlot.py
index e61a11b..7f54028 100644
--- a/Python_script/MeasurementPlot.py
+++ b/Python_script/MeasurementPlot.py
@@ -2,6 +2,7 @@ import pandas as pd
 import matplotlib.pyplot as plt
 import numpy as np
 import time
+from multiprocessing import Queue
 import queue
 import sys
 
@@ -144,7 +145,7 @@ class MeasurementPlot:
 
         plt.rcParams.update({'font.size': 16})
 
-        self.data_queue = queue.Queue(10)
+        self.data_queue = Queue(10)
 
     def stop(self):
         self.data_queue.put((None, None, True))
@@ -160,9 +161,12 @@ class MeasurementPlot:
 
     def drawing_loop(self):
         while True:
+            print('drawing loop getting')
             data_frame, pdf_name, stop_thread = self.data_queue.get()
+            print('drawing loop got. stop is ' +str(stop_thread))
             if stop_thread:
                 return
+            print('drawing loop drawing')
             self.draw_in_this_thread(data_frame, pdf_name='')
 
     def draw_in_this_thread(self, data_frame, pdf_name=''):
diff --git a/Python_script/prototype.py b/Python_script/prototype.py
index cc3a528..a16c1de 100755
--- a/Python_script/prototype.py
+++ b/Python_script/prototype.py
@@ -13,7 +13,7 @@ import json
 import MeasurementPlot
 import sys
 import analysis
-import threading
+from multiprocessing import Process
 import external_sensors
 import PostPlot
 import os
@@ -174,12 +174,15 @@ class Measurements:
         This is a "chamber measurement point". It consists out of multiple measurements sets. The data for all
         measurement sets of this chamber point are taken for this measurement.
         """
-        measurement_thread = threading.Thread(target=self.perform_single_measurement_impl,
-                                              args=(output, target_temp, target_hum, soaking_time,
-                                                    n_stable_reads))
-        measurement_thread.start()
-        self.measurement_plot.drawing_loop()
-        measurement_thread.join()
+        measurement_process = 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)
+        plt.ion()
+        try:
+            self.measurement_plot.drawing_loop()
+        finally:
+            measurement_process.join()
 
     def perform_single_measurement_impl(self, output, target_temp, target_hum, soaking_time, n_stable_reads):
         with open(output, mode='w', newline='') as csv_file:
-- 
GitLab