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

Discussion in 'Python' started by riquito@gmail.com, Jun 14, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Le Mercredi 14 Juin 2006 17:13, 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
    Maric Michaud, Jun 14, 2006
    #2
    1. Advertising

  3. imcs ee Guest

    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, <> 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
    >
    imcs ee, Jun 14, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. enlightened us with:
    > Script2 is not a module, it's a program that _could_ receive input
    > via pipe.


    Then simply send it an EOF manually if it doesn't.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jun 14, 2006
    #5
  6. imcs ee Guest

    yeah, forget my post ,it;s useless.
    sorry for my thoughtless
    On 14 Jun 2006 10:40:15 -0700, <> 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
    >
    imcs ee, Jun 14, 2006
    #6
  7. Steve Holden Guest

    imcs ee wrote:
    > yeah, forget my post ,it;s useless.
    > sorry for my thoughtless
    > On 14 Jun 2006 10:40:15 -0700, <> 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
    Steve Holden, Jun 14, 2006
    #7
  8. Guest

    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, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Tucker

    Piped stream help.

    Andrew Tucker, Oct 5, 2003, in forum: Java
    Replies:
    3
    Views:
    496
    Harald Hein
    Oct 6, 2003
  2. Ryan Stewart

    Wrapping Piped Streams?

    Ryan Stewart, Jan 11, 2004, in forum: Java
    Replies:
    10
    Views:
    1,860
    Ryan Stewart
    Jan 11, 2004
  3. David Kensche
    Replies:
    11
    Views:
    1,093
    David Kensche
    Dec 10, 2004
  4. morrell
    Replies:
    1
    Views:
    934
    roy axenov
    Oct 10, 2006
  5. Glenn
    Replies:
    5
    Views:
    261
    C.DeRykus
    Jan 3, 2011
Loading...

Share This Page