You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a part of python script as follows, these functions call C++ functions.
GenerateMesh()
CloseProject()
GenerateMesh start external QProcess to do real task, then return immediatly. It's async. There is another function reciving QProcess::finished signal to do some other work. CloseProject closes the program.
This design has a fatal problem. GenerateMesh is async, CloseProject is invoked, at this point, the child process is not finished.
A possible solution is move all python operation to subthread, and make GeneratorMesh synchronous. but other function do a lot of UI operation.
Is there a proper way to block or pause python running?
There are multiple ways of doing this of course, What solution you prefer depends on how you design and use Qt and object-orientation in general. However, a simply dirty way it so put a wait before CloseProject where you either wait for the QProcess::finished signal emitted from somewhere (so you would need to have it available) or perhaps just do a wait loop like below. The way I usually block until something is finished is this way:
from PythonQt import QtCore
GenerateMesh()
self._waitLoop = QtCore.QEventLoop()
# QtCore.QTimer.singleShot(5000, self._waitLoop.quit) # Stop waiting after 5 seconds
returnCode = waitLoop.exec_()
self._waitLoop = None
if returnCode == 0:
CloseProject(True)
else:
CloseProject(False)
and then call self._waitLoop.exit(0) either connected to QProcess::finished or from where you handle it. You might of course also want to put a single shot timer on it so make sure you program doesn't hang forever in case QProcess::finished is never signaled - it's in the commented out line above.
Activity
he-hesce commentedon Feb 2, 2024
There are multiple ways of doing this of course, What solution you prefer depends on how you design and use Qt and object-orientation in general. However, a simply dirty way it so put a wait before
CloseProjectwhere you either wait for theQProcess::finishedsignal emitted from somewhere (so you would need to have it available) or perhaps just do a wait loop like below. The way I usually block until something is finished is this way:and then call
self._waitLoop.exit(0)either connected toQProcess::finishedor from where you handle it. You might of course also want to put a single shot timer on it so make sure you program doesn't hang forever in caseQProcess::finishedis never signaled - it's in the commented out line above.