Select in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dio

    Select in Python

    #!/usr/bin/env python2

    from sys import stdin
    from select import select

    while 1:
    (rr, wr, er) = select([stdin], [], [])
    for fd in rr:
    print fd

    program block in the first select(), after I type something and "enter
    ", it never block in select() again,why?

  • Fredrik Lundh

    #2
    Re: Select in Python

    Dio wrote:
    [color=blue]
    > from sys import stdin
    > from select import select
    >
    > while 1:
    > (rr, wr, er) = select([stdin], [], [])
    > for fd in rr:
    > print fd
    >
    > program block in the first select(), after I type something and "enter
    > ", it never block in select() again,why?[/color]

    if there's data waiting to be read from the stream, select() will tell
    you so every time you ask.

    </F>

    Comment

    • K.S.Sreeram

      #3
      Re: Select in Python

      Dio wrote:[color=blue]
      > while 1:
      > (rr, wr, er) = select([stdin], [], [])
      > for fd in rr:
      > print fd
      >
      > program block in the first select(), after I type something and "enter
      > ", it never block in select() again,why?[/color]

      select blocks until there is some data to read from stdin, but it does
      not *clear* the data. so once you enter data, select will not block
      until you read the data. once you're done reading the available data,
      you can again use select to block until more data is available.

      Regards
      Sreeram



      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.2.2 (MingW32)
      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

      iD8DBQFEoM2Ergn 0plK5qqURAm0GAK CxtfwmayK1xmn8o Hq6yi5z4my2hwCg kkta
      67h4Y9lUR9RCC6r QFtrRGgU=
      =zaqF
      -----END PGP SIGNATURE-----

      Comment

      • Gary Herron

        #4
        Re: Select in Python

        Dio wrote:[color=blue]
        > #!/usr/bin/env python2
        >
        > from sys import stdin
        > from select import select
        >
        > while 1:
        > (rr, wr, er) = select([stdin], [], [])
        > for fd in rr:
        > print fd
        >
        > program block in the first select(), after I type something and "enter
        > ", it never block in select() again,why?
        >
        >[/color]
        Because select blocks until there is something to read on stdin, and as
        long as there is something to be read on stdin, it will not block. Since
        your program never reads in the data on stdin, your "enter\n" just sits
        there waiting to be read, and select always inform you of that fact by
        returning stdin in the rr list.

        If you want select to start blocking again, you must read all bytes from
        stdin whenever select says there are bytes to be read.

        Gary Herron

        Comment

        • Dio

          #5
          Re: Select in Python


          K.S.Sreeram 写道:
          [color=blue]
          > Dio wrote:[color=green]
          > > while 1:
          > > (rr, wr, er) = select([stdin], [], [])
          > > for fd in rr:
          > > print fd
          > >
          > > program block in the first select(), after I type something and "enter
          > > ", it never block in select() again,why?[/color]
          >
          > select blocks until there is some data to read from stdin, but it does
          > not *clear* the data. so once you enter data, select will not block
          > until you read the data. once you're done reading the available data,
          > you can again use select to block until more data is available.
          >
          > Regards
          > Sreeram[/color]

          Thanks a lot!I use stdin.readline( ) after select and it's ok :)

          Comment

          • Lawrence D'Oliveiro

            #6
            Re: Select in Python

            In article <mailman.7484.1 151389154.27775 .python-list@python.org >,
            Gary Herron <gherron@island training.com> wrote:
            [color=blue]
            >If you want select to start blocking again, you must read all bytes from
            >stdin whenever select says there are bytes to be read.[/color]

            To add to this, it is a good idea to read the select_tut(2) man page,
            particularly the "SELECT LAW" section. There's a lot of dos and don'ts
            there on how to avoid common bugs.

            Comment

            Working...