multiprocessing in Houdini Python panel

   1284   3   5
User Avatar
Member
1 posts
Joined: 2月 2023
Offline
Hello!
I'm writing a Python Panel for Houdini.
I need to execute Python code asynchronously not blocking the main Qt or Python code, simple multithreading doesn't work (most likely because of the global python interpreter lock).
But when I use python multiprocessing, this works as stand alone widget, but within Houdini, even when using multiprocessing.get_context('spawn'), python will fork and not spawn a new process, yielding a new Houdini copy.

Is it possible to make python multiprocessing work within Houdini (python panel)?

I'm currently running on windows, but in the long run, the panel should work on any plaform.

Thank you in advance for your help.
User Avatar
Member
253 posts
Joined: 7月 2013
Offline
Have you tried using Qt threads? https://realpython.com/python-pyqt-qthread/ [realpython.com]
More code, less clicks.
User Avatar
Member
83 posts
Joined: 7月 2005
Offline
I'm trying to get qthreads running in a python panel, it's been problematic, Are there any samples anywhere of this working?
User Avatar
Member
83 posts
Joined: 7月 2005
Offline
ok, well, if anyone else comes across this thread, here is 1 working bit of code. Thank you for holding the duck.

from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
import time

class VideoThread(QtCore.QThread):
    def setStuff(self, vidscr, butt):
        self.vidscr = vidscr
        self.butt = butt

    def run(self):
        loop = True
        while loop:
            # blink blink
            pm = QtGui.QPixmap(320, 240)
            pm.fill(QtGui.QColor('darkGray'))
            self.vidscr.setPixmap(pm)
            time.sleep(1)
            pm.fill(QtGui.QColor('cyan'))
            self.vidscr.setPixmap(pm)
            time.sleep(1)
            if not self.butt.isChecked():
                loop = False

class vidWidg(QtWidgets.QWidget):
    def buildUI(self):
        self.videoScreen = QtWidgets.QLabel(self)
        self.startButton = QtWidgets.QPushButton("run blinky", self)
        self.startButton.setCheckable(True)
        self.startButton.toggled.connect(self.togg)
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.videoScreen)
        layout.addWidget(self.startButton)
        self.setLayout(layout)
       
    def togg(self, val):
        if val:
            self.thread = VideoThread()
            self.thread.setStuff(self.videoScreen, self.startButton)
            self.thread.start()
        else:
            self.thread.wait()
            self.thread.quit()
           

def createInterface():
     root_widget = vidWidg()
     root_widget.buildUI()

     return root_widget
Edited by ivan - 2023年9月1日 21:06:46
  • Quick Links