473,409 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

popen problem


Hi All!

I don't know if this is a Python problem or not. Here is a snippet:

import os
TERM='\n\r\n\r'
cmd = 'cu -l /dev/cuaa0 -s9600'
pop = os.popen4(cmd,1)
pop[0].write('AT'+TERM) # Ping modem

def readln():
buffer = ''
while True:
c = pop[1].read(1)
if c == '\n':
return buffer
elif (c != '\r') and not (c in TERM):
buffer += c

pop[1].readln()
pop[1].readln()
pop[1].readln()

Here is the problem: on FreeBSD, this works fine. The last readln()
calls will print this:

'Connected.'
''
'OK'

But if I try to do the same on Linux (with /dev/ttyS0 instead of
/dev/cuaa0) I get this:

1. The first read blocks.
2. When I do 'killall cu' in another shell, it starts to read this:

'\0x7Connected.'
'cu: Got interrupt signal'

It seems that the first line ('Connected.') if buffered somewhere. I
guess 'OK' is also buffered but it is not read because of the interrup.
Is it a problem with Linux of Python? Or is it a big difference between
/dev/ttyS0 and /dev/cuaa0?

Thanks in advance,

G

Jul 18 '05 #1
2 3905
Gandalf wrote:

Hi All!

I don't know if this is a Python problem or not. Here is a snippet:

import os
TERM='\n\r\n\r'
cmd = 'cu -l /dev/cuaa0 -s9600'
pop = os.popen4(cmd,1)


You want to use a bufsize of 1? The Python docs say that bufsize is
the third param to popen4, does
pop = os.popen4(cmd,'t',0)
or pop = os.popen4(cmd, bufsize = 0)
a better job?

Mathias
Jul 18 '05 #2
Gandalf <ga*****@geochemsource.com> wrote in message news:<ma***********************************@python .org>...
I don't know if this is a Python problem or not. Here is a snippet:
import os
TERM='\n\r\n\r'
cmd = 'cu -l /dev/cuaa0 -s9600'
pop = os.popen4(cmd,1)
pop[0].write('AT'+TERM) # Ping modem
...
1. The first read blocks.
2. When I do 'killall cu' in another shell, it starts to read this:
'\0x7Connected.'
'cu: Got interrupt signal'
It seems that the first line ('Connected.') if buffered somewhere. I
guess 'OK' is also buffered but it is not read because of the interrup.
Is it a problem with Linux of Python? Or is it a big difference between
/dev/ttyS0 and /dev/cuaa0?


Yes, it's buffer madness. This is a common problem with trying to use
a pipe with a child application that uses the stdio library -- which
is just about everything (does it #include <stdio.h>?) The annoying
thing is that it might seem to work sometimes, but then other times it
fails. Plus it's very platform dependent... When you kill the child
from another shell you are causing it to flush its buffer.
Unfortunately that's the only way that I know that you can force it to
flush the buffer -- not very practical.

Instead you should use my marvelous pexpect module.
http://pexpect.sourceforge.net/
It's fun and educational and it's written in 100% pure, refreshing
Python, so it installs easily with no C extension hassles.

One of the features of the stdio library is that it buffers all input
and output. Normally output is line buffered when a program is
printing to a TTY (your terminal screen). Everytime the program prints
a line-feed the currently buffered data will get printed to your
screen. The problem comes when you connect a pipe. The stdio library
is smart and can tell that it is printing to a pipe instead of a TTY.
In that case it switches from line buffer mode to block buffered. In
this mode the currently buffered data is flushed when the BUFFER IS
FULL NOT WHEN THE CHILD PRINTS A LINE FEED. This causes most
interactive programs to deadlock. Block buffering is more efficient
when writing to disks and pipes. Take the situation where a program
prints a message "Enter your user name:\n" and then waits for you type
type something. In block buffered mode, the stdio library will not put
the message into the pipe even though a linefeed is printed. The
result is that you never receive the message, yet the child
application will sit and wait for you to type a response. Don't
confuse the stdio library buffer of the child with your pipe's buffer.
You could flush the your pipe, but that only flushes your output
stream, whereas you have no control over the stdio library buffer.

You can't force the child to flush it's stdout stream.
You can't force the child to change the buffer state to line
buffering.

Odd, but this is the second time today that I've seen this question
come up...

Yours,
Noah
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bryan | last post by:
i have a batch file that contains these two lines: -- args.bat echo %1 echo %2 when i run args.bat from the command line it works correctly... notice that abc has quotes in the first...
2
by: Sami Viitanen | last post by:
Hello, I was using os.popen function to get CVS command output to string from script.. Same commands worked well with Windows, but Linux seems to have problem with those. Whole CVS commands...
1
by: sam | last post by:
Hi, I try to use popen execute scp, when the user finish password, it will return to the cin input prompt, but the problem is popen does not wait for user finish typing up the password before...
17
by: bastiaannaber | last post by:
I am trying to write a program which uses popen but I have a problem. I want to detect if the program I call with popen has ended. For example: #include <stdio.h> #include <sys/types.h>...
1
by: rveloso | last post by:
Hi all, i'm having a really nasty problem with popen. I have the following code : --------------------- .... FILE *PD; .... sprintf(fname,"/usr/bin/gzip -dc %s/%s",dirname,dp->d_name); .......
2
by: Maarten van Veen | last post by:
A long story made short, I've build a python/cgi website consisting of two pages. Page1 has a html form in which you can input a series of queries. Then via Popen it starts a pythons search script,...
5
by: nic | last post by:
On my system (WinXP) typing the following line into command prompt(cmd.exe) successfully scans the file test1.txt: "c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program...
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt" "http://search.cpan.org" and capture all it's...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with...
15
by: Daniel Klein | last post by:
I'm trying to get popen to work on Windows. Here's a simplified example of what I'm trying to get working: I have a hw.c program as follows: #include <stdio.h> main() { printf ("Hello...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.