help needed in subprocess communicate

2008-06-03 Thread rdabane
I'm trying to perform following type of operation from inside a python
script.
1. Open an application shell (basically a tcl )
2. Run some commands on that shell and get outputs from each command
3. Close the shell


I could do it using communicate if I concatenate all my commands
( separated by newline ) and read all the output in the end. So
basically I could do following sequence:
1. command1 \n command2 \n command 3 \n
2. Read all the output

But I want to perform it interactively.
1. command1
2. read output
3. command2
4. read output ..

Following is my code:

from subprocess import *
p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
o,e = p2.communicate(input='qdl_help \n qdl_read /home/rokit/
svnproject/falcon/chip/blocks/intf/bitsif/rtl/m6_bitsif.qdl_cpp \n
qdl_reg_group_list m6_bitsif')


Please suggest a way to perform it interactively with killing the
process each time I want to communicate with it.

Thanks in advance,
-Rahul.
--
http://mail.python.org/mailman/listinfo/python-list


Help need with subprocess communicate

2008-06-03 Thread rdabane
I'm trying to perform following type of operation from inside a python
script.
1. Open an application shell (basically a tcl )
2. Run some commands on that shell and get outputs from each command
3. Close the shell

I could do it using communicate if I concatenate all my commands
( separated by newline ) and read all the output in the end. So
basically I could do following sequence:
1. command1 \n command2 \n command 3 \n
2. Read all the output

But I want to perform it interactively.
1. command1
2. read output
3. command2
4. read output ..

Following is my code:

from subprocess import *
p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
o,e = p2.communicate(input='qdl_help \n qdl_read  \n
qdl_reg_group_list ')

Please suggest a way to perform it interactively with killing the
process each time I want to communicate with it.

Thanks in advance,
-Rahul.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help need with subprocess communicate

2008-06-03 Thread rdabane
On Jun 3, 5:42 pm, Daniel Klein <[EMAIL PROTECTED]> wrote:
> On Tue, 3 Jun 2008 14:04:10 -0700 (PDT), [EMAIL PROTECTED] wrote:
> >I'm trying to perform following type of operation from inside a python
> >script.
> >1. Open an application shell (basically a tcl )
> >2. Run some commands on that shell and get outputs from each command
> >3. Close the shell
>
> >I could do it using communicate if I concatenate all my commands
> >( separated by newline ) and read all the output in the end. So
> >basically I could do following sequence:
> >1. command1 \n command2 \n command 3 \n
> >2. Read all the output
>
> >But I want to perform it interactively.
> >1. command1
> >2. read output
> >3. command2
> >4. read output ..
>
> >Following is my code:
>
> >from subprocess import *
> >p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
> >o,e = p2.communicate(input='qdl_help \n qdl_read  \n
> >qdl_reg_group_list ')
>
> >Please suggest a way to perform it interactively with killing the
> >process each time I want to communicate with it.
>
> Use
> stdin.write(command + '\n')
> to 'send' data to the sub-process.
>
> Use
> stdout.readline()
> to 'receive' data from the sub-process.
>
> But to use this requires you open the subprocess with:
>
> universal_newlines = True
>
> It assumes that 'command' will be sent with '\n' and received data will come
> in a line at a time. Your Python program needs to know what to expect; you
> are in control.
>
> Alternatively, you can use std.write() and stdout.read() (without
> universal_newlines) but this means you need to create your own IPC protocol
> (like netstrings).
>
> Hope this helps,
>
> Daniel Klein

Hi Daniel,
Thanks for your reply..
I've done exactly as you suggested...but I'm still having problem with
the read...it just gets stuck in
the read ( I think because its a blocking read...)

following is a simple example of problem..please try running it ...

from subprocess import *
p2 =
Popen('python',shell=True,stdin=PIPE,stdout=PIPE,universal_newlines=True)
for i in range(10):
p2.stdin.write('print 10'+'\n')   # Write Command
o = p2.stdout.readline()  # Read Command
print o


I appreciate all your help...

Thanks,
-Rahul Dabane.
--
http://mail.python.org/mailman/listinfo/python-list


How to perform a nonblocking read from a process

2008-06-03 Thread rdabane
Hi,
I'm trying to perform following operation from inside the python
script
1. Open a shell ( start a process )
2. Send command1 to the process
3. Get output from the process
4. Send command2 to the process
5. Get output from the process
..


Following is sample code :

from subprocess import *
p2 = Popen('python',stdin=PIPE,stdout=PIPE,universal_newlines=True)
for i in range(10):
p2.stdin.write('print 10'+'\n')
o,e = p2.stdout.readline()
print o,e


It seems that stdout.readline() is a blocking read and it just gets
stuck their..
How to fix this ..

All the help is appreciated ..

Thanks,
-Rahul.



--
http://mail.python.org/mailman/listinfo/python-list


Re: How to perform a nonblocking read from a process

2008-06-03 Thread rdabane
On Jun 3, 7:53 pm, sturlamolden <[EMAIL PROTECTED]> wrote:
> On Jun 4, 3:20 am, [EMAIL PROTECTED] wrote:
>
> > It seems that stdout.readline() is a blocking read and it just gets
> > stuck their..
> > How to fix this ..
>
> Threads are the simplest remedy for blocking i/o.



> Threads are the simplest remedy for blocking i/o.
I've to admit I'm a newbie to this kind of programming...
what if I have to run thousands of these commands...it doesn't make
sense to create
thousands of threads..
Is there a way that above mentioned piece of code be made to worked...


--
http://mail.python.org/mailman/listinfo/python-list


Re: Help need with subprocess communicate

2008-06-03 Thread rdabane
On Jun 3, 11:23 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Tue, 3 Jun 2008 18:04:40 -0700 (PDT), [EMAIL PROTECTED] declaimed the
> following in comp.lang.python:
>
>
>
> > Hi Daniel,
> > Thanks for your reply..
> > I've done exactly as you suggested...but I'm still having problem with
> > the read...it just gets stuck in
> > the read ( I think because its a blocking read...)
>
> And it is likely blocking because the subprocess is doing buffered
> output -- ie, nothing is available to be read because the output has not
> been flushed.
>
> This is a problem with most programs when run as a subprocess -- it
> is common for stdout, when routed to a pipe or file, to behave as a
> buffered stream that only flushes when some x-bytes have been written;
> unlike stdout to a console which gets flushed on each new-line.
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED]  [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/


Is there way to configure the stdout buffer size so that it flushes
earlier..
Is there a way to make above mentioned piece code working?
--
http://mail.python.org/mailman/listinfo/python-list