Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 run executable in separate process thread
#1
I want to be able to call and run an executable in a separate thread so when running it the Python GUI application does not freeze up.

I'm not sure how to do this.

I have a single button on my gui form for testing called buttonLaunch which when clicked will call a module named 'Launch'. My sample code is below. How can I get the executable to run in separate thread?


#!/usr/bin/python3

#OoeyGUI MESS v2.0
#Copyright 2015 - 2019
#Chris Schneider [SHIFT838]
#http://shift838.99er.net

import os
import os.path
import sys
import subprocess
import serial.tools.list_ports
import xml.etree.ElementTree as ET
import platform

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5 import QtCore, QtGui, QtWidgets, QtSerialPort, uic
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QInputDialog, QLineEdit
from os import path

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"

class Main(QtWidgets.QMainWindow):

    def __init__(self):

        super().__init__()

        self.ui = uic.loadUi(LOCAL_DIR + "test.ui", self)
        self.ui.buttonLaunch.clicked.connect(self._Launch)

        self.show()

    def _Launch(self, clicked):
        os.system("java -jar c:/tiimagetool/tiimagetool.jar")
		
if __name__ == '__main__':

    ogv = "2.0 BETA Build 012919"

    app = QtWidgets.QApplication([])
    gui = Main()
    gui.setWindowTitle("OoeyGUI Mess v" + ogv)
    gui.show()
    sys.exit(app.exec_())


guess I should have searched a bit harder. I was able to use subprocess.Popen(executabe here)
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Quote
#2
With PyQt you should use QProcess instead of subprocess. It will automatically create a separate thread for the process. Here is an example if you want to monitor the output:

#!/usr/bin/python3
import sys
from PyQt5 import QtWidgets, QtCore


class Slave(QtCore.QProcess):
    def __init__(self, parent=None):
        super().__init__()
        self.readyReadStandardOutput.connect(self.stdoutEvent)
        self.readyReadStandardError.connect(self.stderrEvent)

    def stdoutEvent(self):
        stdout = self.readAllStandardOutput()
        self.echo(stdout)

    def stderrEvent(self):
        stderr = self.readAllStandardError()
        self.echo(stderr)

    def echo(self, data):
        data = bytes(data).decode("utf8")
        if data:
            print(data, end="")


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.slave = Slave()
        self.slave.start("/home/user/my_process")


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
Else, it can be as simple as

self.process = QtCore.QProcess()
self.process.start("command")
# or
self.process.startDetached("command")
With start() you can add blocking call (waitForFinished) and monitor the process state(). With startDetached(), it returns a boolean to tell if the process could be launched or not.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Tkinter GUI is unresponsive in a thread sandarbhasthana 2 249 May-06-2019, 03:20 PM
Last Post: sandarbhasthana
  [WxPython] Process finished with exit code gehrenfeld 3 621 Jan-10-2019, 11:21 PM
Last Post: gehrenfeld
  Thread/Signal LittleGrim13 2 670 Nov-30-2018, 02:35 PM
Last Post: LittleGrim13
  [Tkinter] Thread for serial Reading shows an error eabs86 0 428 Sep-20-2018, 01:44 AM
Last Post: eabs86
  [WxPython] Using thread in the wxPython hildogjr 8 1,617 Aug-11-2018, 02:59 AM
Last Post: hildogjr
  Executable [tkinter, cx_oracle] for another pc dimvord 1 562 Jul-19-2018, 03:37 PM
Last Post: nilamo
  [Tkinter] Problem reading entry box in thread LeeMadeux 2 932 Jan-17-2018, 03:15 PM
Last Post: LeeMadeux
  Widget placement issues with tkinter grid thread 1 mgtheboss 2 1,306 Jan-09-2018, 03:59 PM
Last Post: SmokerX
  [Tkinter] Link text field to separate python file SMA 2 1,311 Mar-20-2017, 08:45 PM
Last Post: nilamo
  How to fork a process, kill the child and not kill the parent? neXussT 3 2,974 Mar-04-2017, 09:35 PM
Last Post: neXussT

Forum Jump:


Users browsing this thread: 1 Guest(s)