Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`print()` in a loop doesn't output until the loop is done #825

Open
jean opened this issue Jul 17, 2020 · 2 comments
Open

`print()` in a loop doesn't output until the loop is done #825

jean opened this issue Jul 17, 2020 · 2 comments

Comments

@jean
Copy link

@jean jean commented Jul 17, 2020

I paste some code into the BPython REPL.
It looks like nothing happened, I don't even see the code I pasted, but it is running.

When I interrupt the loop, all this appears at once:

>>> import time
>>> 
>>> # seen_nodes = []
>>> # dupe_nodes = []
>>> counter = 0
>>> for row in col.get_rows():
...     node_id = row.get_property('node_id') 
...     if (node_id in seen_nodes) and (node_id not in dupe_nodes):
...         dupe_nodes.append(node_id)
...     elif node_id not in seen_nodes:
...         seen_nodes.append(node_id)
...     counter += 1
...     if not counter % 2:
...         print(f'{counter:6} | seen: {len(seen_nodes)}, dupes: {len(dupe_nodes)}', flush=True)
...         time.sleep(1)
... 
     2 | seen: 12530, dupes: 100
     4 | seen: 12530, dupes: 102
     6 | seen: 12530, dupes: 104
     8 | seen: 12530, dupes: 106
Traceback (most recent call last):
  File "<input>", line 10, in <module>
    time.sleep(1)
  File "/home/jean/.local/lib/python3.8/site-packages/bpython/curtsiesfrontend/coderunner.py", line 176, in sigint_handler
    raise KeyboardInterrupt()
KeyboardInterrupt
@etiennerichart
Copy link
Contributor

@etiennerichart etiennerichart commented Jul 23, 2020

Hey Jean, I can't reproduce this. Maybe it's something else in the rest of your code. Could you share it with us? Also, what version of bpython are you using?

@thomasballinger
Copy link
Member

@thomasballinger thomasballinger commented Jul 23, 2020

Oh I missed a critical detail here, this is only when you paste the code in.

Here's a smaller repro: copy this code and paste it into bpython.

import time
for i in range(10):
    print(i)
    time.sleep(.1)

print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.