Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help on PyQt4 QProcess

Reply
Thread Tools

Help on PyQt4 QProcess

 
 
Edgar Fuentes
Guest
Posts: n/a
 
      08-19-2011
Dear friends,

I need execute an external program from a gui using PyQt4, to avoid
that hang the main thread, i must connect the signal "finished(int)"
of a QProcess to work properly.

for example, why this program don't work?

from PyQt4.QtCore import QProcess
pro = QProcess() # create QProcess object
pro.connect(pro, SIGNAL('started()'), lambda
x="started"rint(x)) # connect
pro.connect(pro, SIGNAL("finished(int)"), lambda
x="finished"rint(x))
pro.start('python',['hello.py']) # star hello.py program
(contain print("hello world!"))
timeout = -1
pro.waitForFinished(timeout)
print(pro.readAllStandardOutput().data())

output:

started
0
b'hello world!\n'

see that not emit the signal finished(int)

I'm using Python 3.2 and PyQt 4.8.4 under winxp 32bit.


best regards,
 
Reply With Quote
 
 
 
 
Phil Thompson
Guest
Posts: n/a
 
      08-19-2011
On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
<(E-Mail Removed)> wrote:
> Dear friends,
>
> I need execute an external program from a gui using PyQt4, to avoid
> that hang the main thread, i must connect the signal "finished(int)"
> of a QProcess to work properly.
>
> for example, why this program don't work?
>
> from PyQt4.QtCore import QProcess
> pro = QProcess() # create QProcess object
> pro.connect(pro, SIGNAL('started()'), lambda
> x="started"rint(x)) # connect
> pro.connect(pro, SIGNAL("finished(int)"), lambda
> x="finished"rint(x))
> pro.start('python',['hello.py']) # star hello.py program
> (contain print("hello world!"))
> timeout = -1
> pro.waitForFinished(timeout)
> print(pro.readAllStandardOutput().data())
>
> output:
>
> started
> 0
> b'hello world!\n'
>
> see that not emit the signal finished(int)


Yes it is, and your lambda slot is printing "0" which is the return code
of the process.

Phil
 
Reply With Quote
 
 
 
 
Edgar Fuentes
Guest
Posts: n/a
 
      08-19-2011
On Aug 19, 1:56*pm, Phil Thompson <(E-Mail Removed)> wrote:
> On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
>
>
>
>
>
>
>
>
>
> <(E-Mail Removed)> wrote:
> > Dear friends,

>
> > I need execute an external program from a gui using PyQt4, to avoid
> > that hang the main thread, i must connect the signal "finished(int)"
> > of a QProcess to work properly.

>
> > for example, why this program don't work?

>
> > * *from PyQt4.QtCore import QProcess
> > * *pro = QProcess() # create QProcess object
> > * *pro.connect(pro, SIGNAL('started()'), lambda
> > x="started"rint(x)) * * * *# connect
> > * *pro.connect(pro, SIGNAL("finished(int)"), lambda
> > x="finished"rint(x))
> > * *pro.start('python',['hello.py']) * * * *# star hello.py program
> > (contain print("hello world!"))
> > * *timeout = -1
> > * *pro.waitForFinished(timeout)
> > * *print(pro.readAllStandardOutput().data())

>
> > output:

>
> > * *started
> > * *0
> > * *b'hello world!\n'

>
> > see that not emit the signal finished(int)

>
> Yes it is, and your lambda slot is printing "0" which is the return code
> of the process.
>
> Phil


Ok, but the output should be:

started
b'hello world!\n'
finished

no?.

thanks Phil
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      08-19-2011
On Friday, August 19, 2011 12:55:40 PM UTC-7, Edgar Fuentes wrote:
> On Aug 19, 1:56*pm, Phil Thompson
> wrote:
> > On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
> > <(E-Mail Removed)> wrote:
> > > Dear friends,

> >
> > > I need execute an external program from a gui using PyQt4, to avoid
> > > that hang the main thread, i must connect the signal "finished(int)"
> > > of a QProcess to work properly.

> >
> > > for example, why this program don't work?

> >
> > > * *from PyQt4.QtCore import QProcess
> > > * *pro = QProcess() # create QProcess object
> > > * *pro.connect(pro, SIGNAL('started()'), lambda
> > > x="started"rint(x)) * * * *# connect
> > > * *pro.connect(pro, SIGNAL("finished(int)"), lambda
> > > x="finished"rint(x))
> > > * *pro.start('python',['hello.py']) * * * *# star hello.py program
> > > (contain print("hello world!"))
> > > * *timeout = -1
> > > * *pro.waitForFinished(timeout)
> > > * *print(pro.readAllStandardOutput().data())

> >
> > > output:

> >
> > > * *started
> > > * *0
> > > * *b'hello world!\n'

> >
> > > see that not emit the signal finished(int)

> >
> > Yes it is, and your lambda slot is printing "0" which is the return code
> > of the process.
> >
> > Phil

>
> Ok, but the output should be:
>
> started
> b'hello world!\n'
> finished
>
> no?.
>
> thanks Phil


Two issues. First of all, your slot for the finished function does not have the correct prototype, and it's accidentally not throwing an exception because of your unnecessary use of default arguments. Anyway, to fix that, try this:

pro.connect(pro, SIGNAL("finished(int)"), lambda v, x="finished"rint(x))

Notice that it adds an argument to the lambda (v) that accepts the int argument of the signal. If you don't have that argument there, the int argument goes into x, which is why Python prints 0 instead of "finished".

Second, processess run asynchrously, and because of line-buffering, IO can output asynchronously, and so there's no guarantee what order output occurs.. You might try calling the python subprocess with the '-u' switch to force unbuffered IO, which might be enough to force synchronous output (depending on how signal/slot and subprocess semantics are implemented).


Carl Banks
 
Reply With Quote
 
Edgar Fuentes
Guest
Posts: n/a
 
      08-19-2011
On Aug 19, 4:21*pm, Carl Banks <(E-Mail Removed)> wrote:
> On Friday, August 19, 2011 12:55:40 PM UTC-7, Edgar Fuentes wrote:
> > On Aug 19, 1:56*pm, Phil Thompson
> > *wrote:
> > > On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
> > > <(E-Mail Removed)> wrote:
> > > > Dear friends,

>
> > > > I need execute an external program from a gui using PyQt4, to avoid
> > > > that hang the main thread, i must connect the signal "finished(int)"
> > > > of a QProcess to work properly.

>
> > > > for example, why this program don't work?

>
> > > > * *from PyQt4.QtCore import QProcess
> > > > * *pro = QProcess() # create QProcess object
> > > > * *pro.connect(pro, SIGNAL('started()'), lambda
> > > > x="started"rint(x)) * * * *# connect
> > > > * *pro.connect(pro, SIGNAL("finished(int)"), lambda
> > > > x="finished"rint(x))
> > > > * *pro.start('python',['hello.py']) * * * *# star hello..py program
> > > > (contain print("hello world!"))
> > > > * *timeout = -1
> > > > * *pro.waitForFinished(timeout)
> > > > * *print(pro.readAllStandardOutput().data())

>
> > > > output:

>
> > > > * *started
> > > > * *0
> > > > * *b'hello world!\n'

>
> > > > see that not emit the signal finished(int)

>
> > > Yes it is, and your lambda slot is printing "0" which is the return code
> > > of the process.

>
> > > Phil

>
> > Ok, but the output should be:

>
> > * * started
> > * * b'hello world!\n'
> > * * finished

>
> > no?.

>
> > thanks Phil

>
> Two issues. *First of all, your slot for the finished function does nothave the correct prototype, and it's accidentally not throwing an exception because of your unnecessary use of default arguments. *Anyway, to fix that, try this:
>
> pro.connect(pro, SIGNAL("finished(int)"), lambda v, x="finished"rint(x))
>
> Notice that it adds an argument to the lambda (v) that accepts the int argument of the signal. *If you don't have that argument there, the int argument goes into x, which is why Python prints 0 instead of "finished".
>
> Second, processess run asynchrously, and because of line-buffering, IO can output asynchronously, and so there's no guarantee what order output occurs. *You might try calling the python subprocess with the '-u' switch to force unbuffered IO, which might be enough to force synchronous output (depending on how signal/slot and subprocess semantics are implemented).
>
> Carl Banks


Thanks Carl, your intervention was very helpful for me, this solve my
semantic error. I need to study more about signal/slots and process.
 
Reply With Quote
 
Phil Thompson
Guest
Posts: n/a
 
      08-20-2011
On Fri, 19 Aug 2011 14:32:12 -0700 (PDT), Edgar Fuentes
<(E-Mail Removed)> wrote:
> On Aug 19, 4:21*pm, Carl Banks <(E-Mail Removed)> wrote:
>> On Friday, August 19, 2011 12:55:40 PM UTC-7, Edgar Fuentes wrote:
>> > On Aug 19, 1:56*pm, Phil Thompson
>> > *wrote:
>> > > On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
>> > > <(E-Mail Removed)> wrote:
>> > > > Dear friends,

>>
>> > > > I need execute an external program from a gui using PyQt4, to

avoid
>> > > > that hang the main thread, i must connect the signal
>> > > > "finished(int)"
>> > > > of a QProcess to work properly.

>>
>> > > > for example, why this program don't work?

>>
>> > > > * *from PyQt4.QtCore import QProcess
>> > > > * *pro = QProcess() # create QProcess object
>> > > > * *pro.connect(pro, SIGNAL('started()'), lambda
>> > > > x="started"rint(x)) * * * *# connect
>> > > > * *pro.connect(pro, SIGNAL("finished(int)"), lambda
>> > > > x="finished"rint(x))
>> > > > * *pro.start('python',['hello.py']) * * * *# star hello.py

program
>> > > > (contain print("hello world!"))
>> > > > * *timeout = -1
>> > > > * *pro.waitForFinished(timeout)
>> > > > * *print(pro.readAllStandardOutput().data())

>>
>> > > > output:

>>
>> > > > * *started
>> > > > * *0
>> > > > * *b'hello world!\n'

>>
>> > > > see that not emit the signal finished(int)

>>
>> > > Yes it is, and your lambda slot is printing "0" which is the return
>> > > code
>> > > of the process.

>>
>> > > Phil

>>
>> > Ok, but the output should be:

>>
>> > * * started
>> > * * b'hello world!\n'
>> > * * finished

>>
>> > no?.

>>
>> > thanks Phil

>>
>> Two issues. *First of all, your slot for the finished function does not
>> have the correct prototype, and it's accidentally not throwing an
>> exception because of your unnecessary use of default arguments.

*Anyway,
>> to fix that, try this:
>>
>> pro.connect(pro, SIGNAL("finished(int)"), lambda v,
>> x="finished"rint(x))
>>
>> Notice that it adds an argument to the lambda (v) that accepts the int
>> argument of the signal. *If you don't have that argument there, the int
>> argument goes into x, which is why Python prints 0 instead of

"finished".
>>
>> Second, processess run asynchrously, and because of line-buffering, IO
>> can output asynchronously, and so there's no guarantee what order

output
>> occurs. *You might try calling the python subprocess with the '-u'

switch
>> to force unbuffered IO, which might be enough to force synchronous

output
>> (depending on how signal/slot and subprocess semantics are

implemented).
>>
>> Carl Banks

>
> Thanks Carl, your intervention was very helpful for me, this solve my
> semantic error. I need to study more about signal/slots and process.


In which case you should look at the modern, Pythonic connection syntax
rather than the old one...

pro.started.connect(lambda: print("started"))
pro.finished.connect(lambda: print("finished"))

Phil
 
Reply With Quote
 
Edgar Fuentes
Guest
Posts: n/a
 
      08-22-2011
On Aug 20, 4:36*am, Phil Thompson <(E-Mail Removed)> wrote:
> On Fri, 19 Aug 2011 14:32:12 -0700 (PDT), Edgar Fuentes
>
>
>
>
>
>
>
>
>
> <(E-Mail Removed)> wrote:
> > On Aug 19, 4:21*pm, Carl Banks <(E-Mail Removed)> wrote:
> >> On Friday, August 19, 2011 12:55:40 PM UTC-7, Edgar Fuentes wrote:
> >> > On Aug 19, 1:56*pm, Phil Thompson
> >> > *wrote:
> >> > > On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
> >> > > <(E-Mail Removed)> wrote:
> >> > > > Dear friends,

>
> >> > > > I need execute an external program from a gui using PyQt4, to

> avoid
> >> > > > that hang the main thread, i must connect the signal
> >> > > > "finished(int)"
> >> > > > of a QProcess to work properly.

>
> >> > > > for example, why this program don't work?

>
> >> > > > * *from PyQt4.QtCore import QProcess
> >> > > > * *pro = QProcess() # create QProcess object
> >> > > > * *pro.connect(pro, SIGNAL('started()'), lambda
> >> > > > x="started"rint(x)) * * * *# connect
> >> > > > * *pro.connect(pro, SIGNAL("finished(int)"), lambda
> >> > > > x="finished"rint(x))
> >> > > > * *pro.start('python',['hello.py']) * * * *# star hello.py

> program
> >> > > > (contain print("hello world!"))
> >> > > > * *timeout = -1
> >> > > > * *pro.waitForFinished(timeout)
> >> > > > * *print(pro.readAllStandardOutput().data())

>
> >> > > > output:

>
> >> > > > * *started
> >> > > > * *0
> >> > > > * *b'hello world!\n'

>
> >> > > > see that not emit the signal finished(int)

>
> >> > > Yes it is, and your lambda slot is printing "0" which is the return
> >> > > code
> >> > > of the process.

>
> >> > > Phil

>
> >> > Ok, but the output should be:

>
> >> > * * started
> >> > * * b'hello world!\n'
> >> > * * finished

>
> >> > no?.

>
> >> > thanks Phil

>
> >> Two issues. *First of all, your slot for the finished function does not
> >> have the correct prototype, and it's accidentally not throwing an
> >> exception because of your unnecessary use of default arguments.

> *Anyway,
> >> to fix that, try this:

>
> >> pro.connect(pro, SIGNAL("finished(int)"), lambda v,
> >> x="finished"rint(x))

>
> >> Notice that it adds an argument to the lambda (v) that accepts the int
> >> argument of the signal. *If you don't have that argument there, the int
> >> argument goes into x, which is why Python prints 0 instead of

> "finished".
>
> >> Second, processess run asynchrously, and because of line-buffering, IO
> >> can output asynchronously, and so there's no guarantee what order

> output
> >> occurs. *You might try calling the python subprocess with the '-u'

> switch
> >> to force unbuffered IO, which might be enough to force synchronous

> output
> >> (depending on how signal/slot and subprocess semantics are

> implemented).
>
> >> Carl Banks

>
> > Thanks Carl, your intervention was very helpful for me, this solve my
> > semantic error. I need to study more about signal/slots and process.

>
> In which case you should look at the modern, Pythonic connection syntax
> rather than the old one...
>
> * * pro.started.connect(lambda: print("started"))
> * * pro.finished.connect(lambda: print("finished"))
>
> Phil


Pythonic, great!, more straightforward.

Thanks again Phil and Carl.

best regards,

Edgar Fuentes
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help with PyQt4 tabwidget setup Neil Wallace Python 0 05-24-2010 08:29 AM
PyQt4.__file__ gives PyQt4/__init__.py as value wgw Python 1 08-15-2009 09:58 PM
pyqt4 qTableWidget add items help ookrin Python 2 04-18-2009 06:16 PM
QProcess and Mozilla Lusy General Computer Support 0 12-11-2007 10:46 AM
PyQT, QProcess.readyReadStdout() problem aljosa Python 0 07-01-2005 11:55 AM



Advertisments