Commit 1332a71e authored by Djelloul Bekri's avatar Djelloul Bekri

huds in multiprocessing to avoid environ sharing

parent 473a07e2
......@@ -19,6 +19,7 @@ import webbrowser
import copy
import uuid
import requests
import multiprocessing
from Qt import QtGui, QtCore, QtWidgets, QtSvg, QtXml, QtCompat
......@@ -147,17 +148,24 @@ class MgvHudThread(QtCore.QThread):
def run(self):
s = self.script
data = []
s = 'def hudScriptFunction():\n' + '\n'.join(['\t' + x for x in s.split('\n')])
s = '''def hudScriptFunction(return_dict, env):
import os
os.environ = dict(env)
def hudScriptFunctionIn():\n''' + '\n'.join(['\t\t' + x for x in s.split('\n')]) + '''
return_dict["OUTPUT"] = hudScriptFunctionIn()
'''
scope = {}
exec (s, scope)
hudScriptFunction = scope['hudScriptFunction']
old = os.environ
os.environ = self.env
try:
data = hudScriptFunction()
manager = multiprocessing.Manager()
return_dict = manager.dict()
p = multiprocessing.Process(target=hudScriptFunction, args=(return_dict, self.env))
p.start()
p.join()
data = return_dict['OUTPUT']
except:
pass
os.environ = old
data = ['Error in hud evaluation']
if not isinstance(data, list) and not isinstance(data, dict):
data = [data]
self.signal.emit(data)
......@@ -225,11 +233,12 @@ class MgvHudItem(QtWidgets.QGraphicsItem):
def refreshData(self):
"""Execute hud script."""
dico = self.graphview.graph.getVars()
for x in os.environ.keys():
dico[x] = os.environ[x]
dico = mgvDicoReplace(dico)
self.thread.env = dico
dico = dict(os.environ)
dicog = self.graphview.graph.getVars()
for x in dicog:
dico[x] = dicog[x]
dicog = mgvDicoReplace(dicog)
self.thread.env = dicog
self.timer.start(50)
self.thread.start()
......@@ -503,6 +512,7 @@ class MgvGraphView(QtWidgets.QGraphicsView):
self.dummyLinks = []
self.gridx = 70 * self.globalScale
self.gridy = 30 * self.globalScale
self.graph = graph
self.userHud = MgvUserHud(self)
self.huds = [MgvHudItem(self, x) for x in graph.pattern.project.huds]
......@@ -518,7 +528,7 @@ class MgvGraphView(QtWidgets.QGraphicsView):
self.selectBox = None
self.preselect = []
self.oldselect = []
self.graph = graph
for w in self.huds:
if w.hud.getEvent() == 'open':
w.refreshData()
......
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