[PyQt] Need some assistance on QThread

Luke Campagnola lcampagn at email.unc.edu
Sun May 15 14:37:14 BST 2011

If you do want to use threading, then I agree with your approach and
Emmanuel's suggestions. However, you do not need to use threading at all for
this task (in my opinion, you should always avoid using threads if

My approach would be:
1) use subprocess.Popen to execute the command
2) use a timer and Popen.poll() to check for process completion
     or even more simply, just use Popen.wait()

It would also be very simple with this approach to monitor the output of the
process and to allow the user to terminate the process early.


On Wed, May 11, 2011 at 02:07, vijay swaminathan <swavijay at gmail.com> wrote:

> Hi All,
> I'm new bie to python and programming and would like to get some assistance
> on the problem stated below.
> I'm developing a GUI (using pyqt) to run some scripts (which were run on
> command line earlier).
> on the GUI I have a run button which calls a .bat file , which has the list
> of scripts to execute. Once the run button is clicked, the run button is
> disabled as  it invokes a command prompt, calls a .bat file and when I close
> the command prompt, I would like to re-enable the Run button.
> Since I wanted to monitor the command prompt, I thought of invoking the
> command prompt on a thread and monitor the thread. Correct me if I went
> techinically incorrect here.
> Run button on GUI:  - Here I call the runscript ()
>  self.connect(self.run_button, SIGNAL('clicked()'), self.runscript)
> def runscript(self):
>         file_operation.Generate_Bat_File(self.complete_file_path) #
> Generate a bat file.
>         self.timer = QTimer() # invoke timer for starting and monitoring
> the thread.
>         self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData)
>         self.timer.start(1000)
> class RunMonitor(QThread):
>     def __init__(self, parent=None):
>         QThread.__init__(self)
>     def run(self):
>         print 'Invoking Command Prompt..........'
>         subprocess.call(["start", "/DC:\\Scripts",
> "scripts_to_execute.bat"], shell=True)
> def sendData(self):
>         if self.run_timer:
>             run_monitor_object = RunMonitor()
>             print 'Starting the thread...........'
>             run_monitor_object.start()
>         if run_monitor_object.isRunning():
>             print 'The Thread is still Alive .......'
>         if run_monitor_object.isFinished():
>             print 'The Thread is not alive anymore......'
> I doubt the following.
> 1.is this the correct way to monitor the thread? if not any help towards
> this is appreciated.
> 2. Is this the correct way to invoke the command prompt in thread?
> 3. is it safe to assume that as long as the command prompt is alive, the
> thread is active and when command prompt is closed, the thread is no more
> active.
> Please help.....
> Eagerly looking forward for some help....I googled enough but could not
> arrive at a solution.
> --
> Vijay Swaminathan
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110515/b6af80c1/attachment.html>

More information about the PyQt mailing list