Blocking readline() Call?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Brady Drummonds

    Blocking readline() Call?

    Hi, everyone,

    I'm just figuring Python out but have become stalled with the following
    snippit of code: For some reason, the program stalls after the "read line:
    x" for the last line. The 'done reading cycle map' message is never
    generated:

    while 1:
    line = file.readline()
    if line == '':
    continue
    data = line.strip().sp lit(':')
    cycleMap[int(data[0])] = int(data[1])
    i = i + 1
    print ('read line %d' % i)
    file.close()
    print ('done reading cycle map')

    I thought readline() was supposed to be non-blocking. Have I misunderstood
    something?

    Thanks,
    Scott


    --
    Remove ".nospam" from the user ID in my e-mail to reply via e-mail.


  • Peter Hansen

    #2
    Re: Blocking readline() Call?

    Scott Brady Drummonds wrote:[color=blue]
    >
    > I'm just figuring Python out but have become stalled with the following
    > snippit of code: For some reason, the program stalls after the "read line:
    > x" for the last line. The 'done reading cycle map' message is never
    > generated:
    >
    > while 1:
    > line = file.readline()
    > if line == '':
    > continue[/color]

    Correct this: you want "break", not continue.
    [color=blue]
    > data = line.strip().sp lit(':')
    > cycleMap[int(data[0])] = int(data[1])
    > i = i + 1
    > print ('read line %d' % i)
    > file.close()
    > print ('done reading cycle map')
    >
    > I thought readline() was supposed to be non-blocking. Have I misunderstood
    > something?[/color]

    Yes, the problem wasn't where you thought it was. A well-placed print
    statement or two would have showed you that readline was being called
    repeatedly, not blocking.

    -Peter

    Comment

    • Jp Calderone

      #3
      Re: Blocking readline() Call?

      On Mon, Nov 10, 2003 at 11:59:42AM -0800, Scott Brady Drummonds wrote:[color=blue]
      > Hi, everyone,
      >
      > I'm just figuring Python out but have become stalled with the following
      > snippit of code: For some reason, the program stalls after the "read line:
      > x" for the last line. The 'done reading cycle map' message is never
      > generated:
      >
      > while 1:
      > line = file.readline()
      > if line == '':
      > continue[/color]

      I think you mean "break" here, not "continue".

      [color=blue]
      > data = line.strip().sp lit(':')
      > cycleMap[int(data[0])] = int(data[1])
      > i = i + 1
      > print ('read line %d' % i)
      > file.close()
      > print ('done reading cycle map')[/color]


      Another way to write this loop would be:

      for line in file:
      ...

      Jp

      Comment

      • Scott Brady Drummonds

        #4
        Re: Blocking readline() Call?


        "Scott Brady Drummonds" <scott.b.drummo nds.nospam@inte l.com> wrote in
        message news:booqnf$ioc $1@news01.intel .com...[color=blue]
        > Hi, everyone,
        >
        > I'm just figuring Python out but have become stalled with the following
        > snippit of code:[/color]
        [code deleted]

        JP said:[color=blue]
        > I think you mean "break" here, not "continue".[/color]

        Peter said:[color=blue]
        > Correct this: you want "break", not continue[/color]

        Mel said:[color=blue]
        > 'continue' doesn't mean what you think it means. Try 'break'.[/color]

        So, what is it that you people are trying to tell me? Please don't beat
        around the bush.

        Scott

        P.S. Sarcasm aside, thanks to all three of you for pointing out my very
        basic mistake. :)


        Comment

        • Paul Clinch

          #5
          Re: Blocking readline() Call?

          "Scott Brady Drummonds" <scott.b.drummo nds.nospam@inte l.com> wrote in message news:<booqnf$io c$1@news01.inte l.com>...[color=blue]
          > Hi, everyone,
          >
          > I'm just figuring Python out but have become stalled with the following
          > snippit of code: For some reason, the program stalls after the "read line:
          > x" for the last line. The 'done reading cycle map' message is never
          > generated:
          >
          > while 1:
          > line = file.readline()
          > if line == '':
          > continue[/color]

          Perhaps you meant break.
          [color=blue]
          > data = line.strip().sp lit(':')
          > cycleMap[int(data[0])] = int(data[1])
          > i = i + 1
          > print ('read line %d' % i)
          > file.close()
          > print ('done reading cycle map')
          >
          > I thought readline() was supposed to be non-blocking. Have I misunderstood
          > something?
          >
          > Thanks,
          > Scott[/color]

          Regards, Paul Clinch

          Comment

          • Peter Hansen

            #6
            Re: Blocking readline() Call?

            Scott Brady Drummonds wrote:[color=blue]
            >
            > "Scott Brady Drummonds" <scott.b.drummo nds.nospam@inte l.com> wrote in
            > message news:booqnf$ioc $1@news01.intel .com...[color=green]
            > > Hi, everyone,
            > >
            > > I'm just figuring Python out but have become stalled with the following
            > > snippit of code:[/color]
            > [code deleted]
            >
            > JP said:[color=green]
            > > I think you mean "break" here, not "continue".[/color]
            >
            > Peter said:[color=green]
            > > Correct this: you want "break", not continue[/color]
            >
            > Mel said:[color=green]
            > > 'continue' doesn't mean what you think it means. Try 'break'.[/color]
            >
            > So, what is it that you people are trying to tell me? Please don't beat
            > around the bush.[/color]

            Sorry we were unclear, Scott. See Paul Clinch's post for an answer that
            is more concise and perhaps easier for you to understand. ;-) ;-)

            -Peter

            Comment

            Working...