473,399 Members | 2,478 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,399 software developers and data experts.

stdin read delay when using pipes [solved]

2
I'm having some issues when trying to read input off of a pipe using a python script. I'm trying to process packet data from tcpdump in real-time, so it's a filter that needs to read data while the process being piped from is still reading.

On both my Linux and OSX system, it seems that there's a large delay reading from stdin if the process being read from is still running, even if it's producing output to be read. It will wait until it has a really large amount of data, and then suddenly it will read it all, process it all, and then idle again waiting for a huge chunk of data. I think that it's something that either Python or I am doing because when I pipe the output through awk it processes it in real-time.

Anyhow, here's the source for my *very simple* test to try to get the pipe working:

Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     print "input: " + raw_input()
  3.  
The command I'm running is:

sudo tcpdump -i en1 -xx -l |awk '{print $2$3$4$5$6$7$8$9}' | python test.py

That command will hang until there's a good amount of data. Removing the python script from the end produces real-time awk formatted data without delay.

I've also tried this for the script so that I'm only reading a single character at a time, same problem:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. buffer = []
  4. while 1:
  5.     char = sys.stdin.read(1)
  6.     if not char == "\n":
  7.         buffer.append(char)
  8.     else:
  9.         print "input: " + "".join(buffer)
  10.         buffer = []
  11.  
Weirdness, this has gotta be a simple problem that I'm overlooking.
Apr 12 '06 #1
2 5994
ShawnD
2
Found my answer, it would seem that awk is buffering the output and won't flush/write it until it has a bunch when piping to another process. Removing awk and just using tcpdump in line-buffered mode and processing the output with Python seems to do the trick, but it seems to be a hack.

Someone with more Unix-fu than I have may be able to figure out how to get awk to line buffer or flush.
Apr 13 '06 #2
tasoth
1
The 'mawk' implementation of awk has an extra switch for interactive mode: -Wi. This will do what you want. This switch is not in the POSIX spec, but other implementations may have similar options.
Oct 25 '06 #3

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

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: David Bear | last post by:
This may be a dumb question but I'm confused. using command line syntax pipe like this: cat somefile | pyscript In my pyscript I can simply do buffer = sys.stdin.read()
5
by: | last post by:
Maybe I ask something OS depended but maybe not. So I ask: I want to run an external program from my C++ program. I want stdin of external program constructed from a C++ string in my program....
7
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h>...
0
by: Diogo Bastos | last post by:
Hello, I'm fairly used to working with Python but it's the first time I'm trying to use Tkinter so I'm running into a problem. I'm using three python scripts with Tkinter GUIs and a fourth...
4
by: xman | last post by:
I'm looking for a C/C++ library that can get access to the stdin and stdout of a process. I may invoke a external program in a C/C++ program, but how do write data into the stdin of the external...
2
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and...
16
by: fbertasso | last post by:
Hi, I´m opening stdin to get a file and pass it through a pipe. razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor", "w"); while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {...
5
by: Luis Zarrabeitia | last post by:
I have a problem with this piece of code: ==== import sys for line in sys.stdin: print "You said!", line ==== Namely, it seems that the stdin buffers the input, so there is no reply until ...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.