From f521fae1525f5f7cf2479e5e38444995abb9bb8d Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Fri, 15 Feb 2019 15:11:09 +0100 Subject: [PATCH] Fix call_on_qt_thread decorator CURA-6225 Do thread check in the wrapper function, not outside. --- cura/Utils/Threading.py | 9 ++++++++- plugins/UFPWriter/UFPWriter.py | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cura/Utils/Threading.py b/cura/Utils/Threading.py index 3cd6200513..550a5421ff 100644 --- a/cura/Utils/Threading.py +++ b/cura/Utils/Threading.py @@ -1,3 +1,4 @@ +import functools import threading from cura.CuraApplication import CuraApplication @@ -6,7 +7,7 @@ from cura.CuraApplication import CuraApplication # # HACK: # -# In project loading, when override the existing machine is selected, the stacks and containers that are correctly +# In project loading, when override the existing machine is selected, the stacks and containers that are currently # active in the system will be overridden at runtime. Because the project loading is done in a different thread than # the Qt thread, something else can kick in the middle of the process. One of them is the rendering. It will access # the current stacks and container, which have not completely been updated yet, so Cura will crash in this case. @@ -22,7 +23,13 @@ class InterCallObject: def call_on_qt_thread(func): + @functools.wraps(func) def _call_on_qt_thread_wrapper(*args, **kwargs): + # If the current thread is the main thread, which is the Qt thread, directly call the function. + current_thread = threading.current_thread() + if isinstance(current_thread, threading._MainThread): + return func(*args, **kwargs) + def _handle_call(ico, *args, **kwargs): ico.result = func(*args, **kwargs) ico.finish_event.set() diff --git a/plugins/UFPWriter/UFPWriter.py b/plugins/UFPWriter/UFPWriter.py index c0db104c82..a686837b30 100644 --- a/plugins/UFPWriter/UFPWriter.py +++ b/plugins/UFPWriter/UFPWriter.py @@ -44,8 +44,12 @@ class UFPWriter(MeshWriter): # trigger loading other containers. Because those loaded containers are QtObjects, they must be created on the # Qt thread. The File read/write operations right now are executed on separated threads because they are scheduled # by the Job class. - @call_on_qt_thread def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode): + print("------------> self _write = ", self._write) + return self._write(stream, nodes, mode = mode) + + @call_on_qt_thread + def _write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode): archive = VirtualFile() archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)