Friday, 9 August 2019

Stopping PyQGIS script that has infinite loop, using keyboard?



So I wrote a script that does what I want over and over again using a "while True:" in a certain time interval (every 5 seconds using time.sleep(5)). So far so good, BUT when I want to stop it I just can't.


I have tried Control+C, Control+Break, Escape and it practically ignores my keyboard. The only way to stop it is to close QGIS. Any ideas? Furthermore, when the script hits time.sleep(5) QGIS kind of lags and freezes for 5 seconds and I can not, for example, pan the layer but I assume this is normal.


Here is my script:


from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface
import time



while True:

def change_color():
active_layer = iface.activeLayer()
pipeline=[]
txt=open('C:/users/stelios/desktop/project/Sensor.txt','r')
for line in txt.readlines():
pipeline.append(line.split())
print pipeline
pipeline2=[]

for label,color in pipeline:
if "0" in color:
pipeline2.append([label,"green"])
else:
pipeline2.append([label,"red"])

print pipeline2
elatomatikoi=""
categories=[]


for label,color in pipeline2:
if 'red' in color:
elatomatikoi=elatomatikoi + label+","
symbol = QgsSymbolV2.defaultSymbol(active_layer.geometryType())
symbol.setColor(QColor(color))
category = QgsRendererCategoryV2(int(label), symbol, label)
categories.append(category)

expression = 'id'
renderer = QgsCategorizedSymbolRendererV2(expression, categories)

active_layer.setRendererV2(renderer)
active_layer.setCacheImage(None)
iface.mapCanvas().refresh()
iface.legendInterface().refreshLayerSymbology(active_layer)
elatomatikoi= elatomatikoi[:-1]

for label,color in pipeline2:
if 'red' in color:
QMessageBox.critical(None,"Warning",("Leakage at pipe(s):%s\nCheck Pipeline status " %elatomatikoi))
break

txt.close()

change_color()
time.sleep(5)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...