473,385 Members | 1,355 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,385 software developers and data experts.

tail -f sys.stdin

Hi!

Maybe a very newbie question but:
I'd like to write a prog which reads one line at a time on its sys.stdin
and immediately processes it.
If there are'nt any new lines wait (block on input).

I couldn't find a solution for it.
Several methods that doesn't fit here:
- reading the entire file-like object until EOF and then process
- read one line(all lines) and when there aren't any lines more, quit.

So I need a 'tail -f' for stdin.

Thanks.

--
--arutz
Jul 19 '05 #1
2 2913
Antal Rutz <ar***@mimoza.pantel.net> wrote:
Hi!

Maybe a very newbie question but:
I'd like to write a prog which reads one line at a time on its sys.stdin
and immediately processes it.
If there are'nt any new lines wait (block on input).


what about:

for line in sys.stdin:
process(line)

--
-----------------------------------------------------------
| Radovan GarabĂ*k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
Jul 19 '05 #2
garabik:
what about:

for line in sys.stdin:
process(line)


This does not meet the OP's requirement, which was
I'd like to write a prog which reads one line at a time on its sys.stdin
and immediately processes it.
If there are'nt any new lines wait (block on input).


It's a subtle difference. The implementation of iter(file)
reads a block of data at a time and breaks that into lines,
along with the logic to read another block as needed. If
there isn't yet enough data for the block then Python will
sit there waiting.

The OP already found the right solution which is to call
the "readline()" method.

Compare the timestamps in the following

% ( echo "a" ; sleep 2 ; echo "b" ) | python -c "import sys, time\
for line in sys.stdin:\
print time.time(), repr(line)"

1118335675.45 'a\n'
1118335675.45 'b\n'
% ( echo "a" ; sleep 2 ; echo "b" ) | python -c "import sys, time\
while 1:\
line = sys.stdin.readline()\
if not line: break \
print time.time(), repr(line)"
1118335678.56 'a\n'
1118335680.28 'b\n'
%

Andrew
da***@dalkescientific.com

Jul 19 '05 #3

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

Similar topics

1
by: Stephen Thorne | last post by:
Decorators have been getting lots of air-time at the moment, but only really the syntax. After a short discussion on irc the other night I decided to download python2.4 from experimental and write...
10
by: Mel | last post by:
i need to create a unix like "tail" function on my site. the question: the text is displayed in a scrolled area and a new line is added at the end and the entire text is scrolled down so that...
12
by: s99999999s2003 | last post by:
hi I have a file which is very large eg over 200Mb , and i am going to use python to code a "tail" command to get the last few lines of the file. What is a good algorithm for this type of task...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
30
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API,...
21
by: Owen Zhang | last post by:
What is the best way to implement "tail -f" in C or C++ and higher performance compared to either unix shell command "tail -f" or perl File::Tail ? Any suggestion appreciated. Thanks.
6
by: lak | last post by:
i want to write a program that implement tail and tail -f command in unix. can any one help me in this?
3
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.