[PyQt] Avoiding freeze of the application

Brian Kelley kelley at eyesopen.com
Sat Feb 7 18:06:23 GMT 2009

If you don't want to use QProcess, you can use subprocess.Popen and the poll() method.

This would be something like:

popen =  Subprocess.Popen("plink -batch %s@%s echo" %
(self.beqLineEdit.text(), self.linuxmachineComboBox.currentText())
                                      , shell=True
                                      , stderr=errptr)

And in some QTimer bound method:

retcode = popen.poll()
if retcode is not None:
  # process has finished

QProcess is nice because it already has a signal that it sends when the process terminates.  subprocess.Popen also has some quirks like you can't use poll.wait or os.wait and popen.poll at the same time.


On 2/7/09 11:00 AM, "Phil Thompson" <phil at riverbankcomputing.com> wrote:

On Sat, 07 Feb 2009 16:53:27 +0100, Geert Vancompernolle
<geert.discussions at gmail.com> wrote:
> Hi,
> I want to achieve the following:
> * My (Windows) application is trying to make a connection with a Linux
> server, using another application (plink, part of the Putty distro)
> * Since an external application is to be called, I'm using the method
> subprocess in this way:
>             retcode = subprocess.call( "plink -batch %s@%s echo" %
> (self.beqLineEdit.text(), self.linuxmachineComboBox.currentText())
>                                      , shell=True
>                                      , stderr=errptr
>                                      )
> * However, that call can take up to 10 seconds (worst case).  In the
> mean time, my main application is "frozen", I can't do anything else.
> Now, what I would like to do, is to decouple the above call from the
> main thread, such that the main application becomes "free" again.  I
> also would like to start a one shot timer (using QTimer.singleShot()) to
> create a time-out.  This is to prevent a "hang" of the application, in
> case something goes wrong during the subprocess call.
> So, my intention is to check when the one shot timer elapses, if the
> subprocess call is still busy.  If not, then all is fine and I simply
> ignore the time out.  If the subprocess is still busy, I would like to
> be able to (if needed, forcefully) stop the subprocess call.
> I currently started the one shot timer just before I launched the
> subprocess call, but I see that the one shot timer is also blocked by
> the subprocess call.  So, that doesn't do what I in fact want to do.
> My questions:
> 1. How can I "decouple" the subprocess call?
> 2. How can I forcefully stop a subprocess call (that should be the case
> if my one shot timer elapses after 10 seconds, and the subprocess call
> is not returned yet)?
> 3. What's the best approach to achieve the above requirements?  Using a
> kind of a state machine, where I first start the subprocess call
> (decoupled), then start the one shot timer, change the state and then
> check in that state if the subprocess call has indeed ended?  And if
> not, forcefully stop the subprocess call?
> Any practical helpful tips much appreciated!

Use QProcess instead of subprocess.

PyQt mailing list    PyQt at riverbankcomputing.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090207/594561a7/attachment-0001.html

More information about the PyQt mailing list