Skip to content

How to block python running using PythonQt #187

Description

@NessajHu

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?

Activity

  1. he-hesce commented on Feb 2, 2024

    @he-hesce
    Contributor

    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.

  2. locked and limited conversation to collaborators on Feb 2, 2024
  3. converted this issue into a discussion #188 on Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions