Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11858 > unrolled thread

Help on PyQt4 QProcess

Started byEdgar Fuentes <fuentesej@gmail.com>
First post2011-08-19 10:15 -0700
Last post2011-08-22 10:43 -0700
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Help on PyQt4 QProcess Edgar Fuentes <fuentesej@gmail.com> - 2011-08-19 10:15 -0700
    Re: Help on PyQt4 QProcess Phil Thompson <phil@riverbankcomputing.com> - 2011-08-19 18:56 +0100
      Re: Help on PyQt4 QProcess Edgar Fuentes <fuentesej@gmail.com> - 2011-08-19 12:55 -0700
        Re: Help on PyQt4 QProcess Carl Banks <pavlovevidence@gmail.com> - 2011-08-19 13:21 -0700
          Re: Help on PyQt4 QProcess Edgar Fuentes <fuentesej@gmail.com> - 2011-08-19 14:32 -0700
            Re: Help on PyQt4 QProcess Phil Thompson <phil@riverbankcomputing.com> - 2011-08-20 09:36 +0100
              Re: Help on PyQt4 QProcess Edgar Fuentes <fuentesej@gmail.com> - 2011-08-22 10:43 -0700

#11858 — Help on PyQt4 QProcess

FromEdgar Fuentes <fuentesej@gmail.com>
Date2011-08-19 10:15 -0700
SubjectHelp on PyQt4 QProcess
Message-ID<54614b21-2c80-444c-8ac5-89fc52b77121@fv14g2000vbb.googlegroups.com>
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":print(x))        # connect
   pro.connect(pro, SIGNAL("finished(int)"), lambda
x="finished":print(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,

[toc] | [next] | [standalone]


#11862

FromPhil Thompson <phil@riverbankcomputing.com>
Date2011-08-19 18:56 +0100
Message-ID<mailman.229.1313777088.27778.python-list@python.org>
In reply to#11858
On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
<fuentesej@gmail.com> 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":print(x))        # connect
>    pro.connect(pro, SIGNAL("finished(int)"), lambda
> x="finished":print(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

[toc] | [prev] | [next] | [standalone]


#11870

FromEdgar Fuentes <fuentesej@gmail.com>
Date2011-08-19 12:55 -0700
Message-ID<607dd6a4-7862-4df6-bd1e-e9f382739592@fv14g2000vbb.googlegroups.com>
In reply to#11862
On Aug 19, 1:56 pm, Phil Thompson <p...@riverbankcomputing.com> wrote:
> On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
>
>
>
>
>
>
>
>
>
> <fuente...@gmail.com> 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":print(x))        # connect
> >    pro.connect(pro, SIGNAL("finished(int)"), lambda
> > x="finished":print(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

[toc] | [prev] | [next] | [standalone]


#11875

FromCarl Banks <pavlovevidence@gmail.com>
Date2011-08-19 13:21 -0700
Message-ID<9eb754a9-d002-4b05-98ed-afd242e2ee58@glegroupsg2000goo.googlegroups.com>
In reply to#11870
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
> > <fuen...@gmail.com> 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":print(x))        # connect
> > >    pro.connect(pro, SIGNAL("finished(int)"), lambda
> > > x="finished":print(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":print(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

[toc] | [prev] | [next] | [standalone]


#11886

FromEdgar Fuentes <fuentesej@gmail.com>
Date2011-08-19 14:32 -0700
Message-ID<fc9a4e92-6975-48ac-b7d9-363be6f7a26d@m18g2000vbl.googlegroups.com>
In reply to#11875
On Aug 19, 4:21 pm, Carl Banks <pavlovevide...@gmail.com> 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
> > > <fuen...@gmail.com> 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":print(x))        # connect
> > > >    pro.connect(pro, SIGNAL("finished(int)"), lambda
> > > > x="finished":print(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":print(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.

[toc] | [prev] | [next] | [standalone]


#11904

FromPhil Thompson <phil@riverbankcomputing.com>
Date2011-08-20 09:36 +0100
Message-ID<mailman.254.1313829368.27778.python-list@python.org>
In reply to#11886
On Fri, 19 Aug 2011 14:32:12 -0700 (PDT), Edgar Fuentes
<fuentesej@gmail.com> wrote:
> On Aug 19, 4:21 pm, Carl Banks <pavlovevide...@gmail.com> 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
>> > > <fuen...@gmail.com> 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":print(x))        # connect
>> > > >    pro.connect(pro, SIGNAL("finished(int)"), lambda
>> > > > x="finished":print(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":print(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

[toc] | [prev] | [next] | [standalone]


#12043

FromEdgar Fuentes <fuentesej@gmail.com>
Date2011-08-22 10:43 -0700
Message-ID<bb896b53-88d1-4470-86b8-e04d6e5e2bce@z7g2000vbp.googlegroups.com>
In reply to#11904
On Aug 20, 4:36 am, Phil Thompson <p...@riverbankcomputing.com> wrote:
> On Fri, 19 Aug 2011 14:32:12 -0700 (PDT), Edgar Fuentes
>
>
>
>
>
>
>
>
>
> <fuente...@gmail.com> wrote:
> > On Aug 19, 4:21 pm, Carl Banks <pavlovevide...@gmail.com> 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
> >> > > <fuen...@gmail.com> 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":print(x))        # connect
> >> > > >    pro.connect(pro, SIGNAL("finished(int)"), lambda
> >> > > > x="finished":print(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":print(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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web