Blocking readline() Call?

  • Thread starter Scott Brady Drummonds
  • Start date
S

Scott Brady Drummonds

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().split(':')
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
 
J

Jp Calderone

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

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

data = line.strip().split(':')
cycleMap[int(data[0])] = int(data[1])
i = i + 1
print ('read line %d' % i)
file.close()
print ('done reading cycle map')


Another way to write this loop would be:

for line in file:
...

Jp
 
P

Peter Hansen

Scott said:
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

Correct this: you want "break", not continue.
data = line.strip().split(':')
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?

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
 
S

Scott Brady Drummonds

Scott Brady Drummonds said:
Hi, everyone,

I'm just figuring Python out but have become stalled with the following
snippit of code:
[code deleted]

JP said:
I think you mean "break" here, not "continue".

Peter said:
Correct this: you want "break", not continue

Mel said:
'continue' doesn't mean what you think it means. Try 'break'.

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. :)
 
P

Paul Clinch

Scott Brady Drummonds said:
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

Perhaps you meant break.
data = line.strip().split(':')
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

Regards, Paul Clinch
 
P

Peter Hansen

Scott said:
Scott Brady Drummonds said:
Hi, everyone,

I'm just figuring Python out but have become stalled with the following
snippit of code:
[code deleted]

JP said:
I think you mean "break" here, not "continue".

Peter said:
Correct this: you want "break", not continue

Mel said:
'continue' doesn't mean what you think it means. Try 'break'.

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top