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

prog1 | prog2 . How not to make prog2 block if not piped?

I googled around, but couldn't understand how to solve this problem.
I have 2 scripts

# script1.py #
print 'something'

#script2.py
x=sys.stdin.read()
print 'passed'

if I run
script1.py | script2.py
all goes well.

But if I run just
script2.py
the program blocks waiting forever for input.

On *nix I used select.select to solve this problem, but on windows?
I read that maybe I should use, from win32api, GetStdHandle and
WaitForMultipleObjects, but how to do it it's far from my knowledge.

Any help?

Thank you,
Riccardo

Jun 14 '06 #1
6 1148
Le Mercredi 14 Juin 2006 17:13, ri*****@gmail.com a écrit*:
# script1.py #
print 'something'

#script2.py
x=sys.stdin.read() read() will read a file object to the end, i guess you want to use readline()
instead or the builtin raw_input.
print 'passed'

if I run
script1.py | script2.py
all goes well. This is because when script1.py ends, it will send an 'EOF' char to
script2.py, wich terminate the read method.

But if I run just
script2.py
the program blocks waiting forever for input.

here, newlines ('\n'), doesn't terminate the read call, you can stop the read
by typing ctrl+d at the beginnning of a new line in a normal unix terminal.
--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Jun 14 '06 #2
do u really need read something even when you run the scripts2.py directly?
why not just change script2.py to
#script2.py
if __name__ == "__main__":
x=sys.stdin.read()
print 'passed'
else:
print 'passed from else branch'

is it what you want? or anything i misunderstand.

On 14 Jun 2006 08:13:04 -0700, ri*****@gmail.com <ri*****@gmail.com> wrote:
I googled around, but couldn't understand how to solve this problem.
I have 2 scripts

# script1.py #
print 'something'

#script2.py
x=sys.stdin.read()
print 'passed'

if I run
script1.py | script2.py
all goes well.

But if I run just
script2.py
the program blocks waiting forever for input.

On *nix I used select.select to solve this problem, but on windows?
I read that maybe I should use, from win32api, GetStdHandle and
WaitForMultipleObjects, but how to do it it's far from my knowledge.

Any help?

Thank you,
Riccardo

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

Jun 14 '06 #3

imcs ee ha scritto:
do u really need read something even when you run the scripts2.py directly?
why not just change script2.py to
#script2.py
if __name__ == "__main__":
x=sys.stdin.read()
print 'passed'
else:
print 'passed from else branch'

is it what you want? or anything i misunderstand.


it won't do. clever btw.
Script2 is not a module, it's a program that _could_ receive input via
pipe.

Jun 14 '06 #4
yeah, forget my post ,it;s useless.
sorry for my thoughtless
On 14 Jun 2006 10:40:15 -0700, ri*****@gmail.com <ri*****@gmail.com> wrote:

imcs ee ha scritto:
do u really need read something even when you run the scripts2.py directly?
why not just change script2.py to
#script2.py
if __name__ == "__main__":
x=sys.stdin.read()
print 'passed'
else:
print 'passed from else branch'

is it what you want? or anything i misunderstand.


it won't do. clever btw.
Script2 is not a module, it's a program that _could_ receive input via
pipe.

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

Jun 14 '06 #5
imcs ee wrote:
yeah, forget my post ,it;s useless.
sorry for my thoughtless
On 14 Jun 2006 10:40:15 -0700, ri*****@gmail.com <ri*****@gmail.com> wrote:
imcs ee ha scritto:

do u really need read something even when you run the scripts2.py directly?
why not just change script2.py to
#script2.py
if __name__ == "__main__":
x=sys.stdin.read()
print 'passed'
else:
print 'passed from else branch'

is it what you want? or anything i misunderstand.


it won't do. clever btw.
Script2 is not a module, it's a program that _could_ receive input via
pipe.

It really doesn't matter *how* it receives input, whether from a pipe,
or a terminal or a redirected file.

When you run it "standalone" you should give it some input - type some
text then enter ^D (on Unix-like systems) or ^Z (on Windows). How else
do you expect read() to return anything? It *has* to read to the end fo
the file before it returns a value.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 14 '06 #6

Steve Holden ha scritto:
When you run it "standalone" you should give it some input - type some
text then enter ^D (on Unix-like systems) or ^Z (on Windows). How else
do you expect read() to return anything? It *has* to read to the end fo
the file before it returns a value.

regards
Steve


you've got reason.
I must read my command line options, if then there is still text I use
that, otherwise I read from stdin. If nothing was piped, the user must
complete the work.

Thank you all,
Riccardo

Jun 15 '06 #7

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

Similar topics

2
by: FiremanSAM | last post by:
Hai all, I would make a ip block script for my site. The blocked ip are stored in a file. I have made the following script. Why does this not work. The file entries must be stored in a array...
2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the...
9
by: Michael Mair | last post by:
Hello, in C89 (at least in the last public draft), "3.6.2 Compound statement, or block", we have ,--- | Syntax | | compound-statement: | { ...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
15
by: Fariba | last post by:
Hello , I am trying to call a mthod with the following signature: AddRole(string Group_Nam, string Description, int permissionmask); Accroding to msdn ,you can mask the permissions using...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
16
by: nleahcim | last post by:
Hi - I am starting to look at doing something that I think will be amazingly difficult. I ideally would like to do this in C, as that is what I am best in. I can handle C++ but I am not particuarly...
11
by: HowardL | last post by:
Hi, I would like to be able to either pipe data into my prog from stdin or get data from a file. If I try to check by using if( Console.In.Peek() != -1 ) { Console.WriteLine("GOT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.