[PyQt] Avoiding freeze of the application

Phil Thompson phil at riverbankcomputing.com
Sat Feb 7 16:00:27 GMT 2009


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.

Phil


More information about the PyQt mailing list