Commit 26ef709d authored by Lars Bittrich's avatar Lars Bittrich

Bugfix to improve thread safety in opticalscan; it now uses queue and event instead of just pipes

parent c6633317
...@@ -21,7 +21,8 @@ If not, see <https://www.gnu.org/licenses/>. ...@@ -21,7 +21,8 @@ If not, see <https://www.gnu.org/licenses/>.
from PyQt5 import QtCore, QtWidgets from PyQt5 import QtCore, QtWidgets
import numpy as np import numpy as np
from multiprocessing import Process, Pipe from multiprocessing import Process, Queue, Event
import queue
from imagestitch import imageStacking from imagestitch import imageStacking
import os import os
import cv2 import cv2
...@@ -30,7 +31,7 @@ from time import sleep, time ...@@ -30,7 +31,7 @@ from time import sleep, time
import datetime import datetime
import sys import sys
def scan(path, sol, zpositions, grid, controlclass, connection, ishdr=False): def scan(path, sol, zpositions, grid, controlclass, dataqueue, stopevent, ishdr=False):
if ishdr: if ishdr:
merge_mertens = cv2.createMergeMertens() merge_mertens = cv2.createMergeMertens()
with open("scanlog.txt", "a") as fp: with open("scanlog.txt", "a") as fp:
...@@ -63,16 +64,11 @@ def scan(path, sol, zpositions, grid, controlclass, connection, ishdr=False): ...@@ -63,16 +64,11 @@ def scan(path, sol, zpositions, grid, controlclass, connection, ishdr=False):
cv2imwrite_fix(os.path.join(path,name), res_mertens_8bit) cv2imwrite_fix(os.path.join(path,name), res_mertens_8bit)
else: else:
ramanctrl.saveImage(os.path.join(path,name)) ramanctrl.saveImage(os.path.join(path,name))
if connection.poll(): if stopevent.is_set():
instruction = connection.recv() ramanctrl.disconnect()
if instruction=="stop": return
ramanctrl.disconnect() dataqueue.put(i)
return
connection.send(i)
ramanctrl.disconnect() ramanctrl.disconnect()
while not connection.poll():
sleep(.1)
connection.recv()
def loadAndPasteImage(srcnames, fullimage, fullzval, width, height, def loadAndPasteImage(srcnames, fullimage, fullzval, width, height,
rotationvalue, p0, p1, p): rotationvalue, p0, p1, p):
...@@ -295,8 +291,10 @@ class OpticalScan(QtWidgets.QWidget): ...@@ -295,8 +291,10 @@ class OpticalScan(QtWidgets.QWidget):
QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes: if reply == QtWidgets.QMessageBox.Yes:
self.timer.stop() self.timer.stop()
self.connection.send("stop") self.processstopevent.set()
self.process.join() self.process.join()
self.dataqueue.close()
self.dataqueue.join_thread()
self.view.unblockUI() self.view.unblockUI()
else: else:
return return
...@@ -464,10 +462,11 @@ class OpticalScan(QtWidgets.QWidget): ...@@ -464,10 +462,11 @@ class OpticalScan(QtWidgets.QWidget):
if reply == QtWidgets.QMessageBox.Yes: if reply == QtWidgets.QMessageBox.Yes:
self.prun.setEnabled(False) self.prun.setEnabled(False)
self.ramanctrl.disconnect() self.ramanctrl.disconnect()
parent_conn, child_conn = Pipe() self.processstopevent = Event()
self.connection = parent_conn self.dataqueue = Queue()
self.process = Process(target=scan, args=(path, sol, self.dataset.zpositions, self.process = Process(target=scan, args=(path, sol, self.dataset.zpositions,
self.dataset.grid, self.ramanctrl.__class__, child_conn, self.hdrcheck.isChecked())) self.dataset.grid, self.ramanctrl.__class__,
self.dataqueue, self.processstopevent, self.hdrcheck.isChecked()))
self.process.start() self.process.start()
self.starttime = time() self.starttime = time()
self.progresstime.setEnabled(True) self.progresstime.setEnabled(True)
...@@ -490,8 +489,12 @@ class OpticalScan(QtWidgets.QWidget): ...@@ -490,8 +489,12 @@ class OpticalScan(QtWidgets.QWidget):
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def checkOnScan(self): def checkOnScan(self):
if self.connection.poll(): try:
i = self.connection.recv() i = self.dataqueue.get_nowait()
except queue.Empty:
i = -1
if i>=0:
Ngrid = len(self.dataset.grid) Ngrid = len(self.dataset.grid)
names = [] names = []
for k in range(len(self.dataset.zpositions)): for k in range(len(self.dataset.zpositions)):
...@@ -509,11 +512,11 @@ class OpticalScan(QtWidgets.QWidget): ...@@ -509,11 +512,11 @@ class OpticalScan(QtWidgets.QWidget):
self.progresstime.setText(self.timelabeltext + str(datetime.timedelta(seconds=round(time2go)))) self.progresstime.setText(self.timelabeltext + str(datetime.timedelta(seconds=round(time2go))))
self.imageUpdate.emit() self.imageUpdate.emit()
if i==Ngrid-1: if i==Ngrid-1:
self.connection.send("stop")
cv2imwrite_fix(self.dataset.getImageName(), cv2.cvtColor(self.view.imgdata, cv2.COLOR_RGB2BGR)) cv2imwrite_fix(self.dataset.getImageName(), cv2.cvtColor(self.view.imgdata, cv2.COLOR_RGB2BGR))
self.dataset.saveZvalImg() self.dataset.saveZvalImg()
self.process.join() self.process.join()
self.connection.close() self.dataqueue.close()
self.dataqueue.join_thread()
self.ramanctrl.connect() self.ramanctrl.connect()
self.view.saveDataSet() self.view.saveDataSet()
self.view.unblockUI() self.view.unblockUI()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment