sys.stdout.flush not work in jupyter notebook

流过昼夜 提交于 2019-12-11 06:27:16

问题


So I only want to re-run code from this repo: https://github.com/dennybritz/reinforcement-learning/blob/master/MC/MC%20Prediction%20Solution.ipynb

My focus is on the print's part:

 for i_episode in range(1, num_episodes + 1):
        # Print out which episode we're on, useful for debugging.
        if i_episode % 1000 == 0:
            print "\rEpisode {}/{}.".format(i_episode, num_episodes)
            sys.stdout.flush()

He is using sys.stdout.flush() to create a simple "progress" output. You can see the output from his repo it only show the last Episode iteration 10000/10000 because using sys.stdout.flush()

But when I try to run it in my jupyter notebook (I run using cmd command jupyter notebook) I think sys.stdout.flush() not works, it show every printed iterations, not overwrite the previous one:

Episode 1000/10000.
Episode 2000/10000.
Episode 3000/10000.
Episode 4000/10000.
Episode 5000/10000.
Episode 6000/10000.
Episode 7000/10000.
Episode 8000/10000.
Episode 9000/10000.
Episode 10000/10000.

Am I missing something when run jupyter to make it works?


回答1:


Charless Duffy comment makes everything clear. The problem of overwriting is not on flush function buat in print function.

I found the solution is just edit the print format from:

print("\rEpisode {}/{}.".format(i_episode, num_episodes))

to

print("\rEpisode {}/{}.".format(i_episode, num_episodes), end="")

and the overwrite works now



来源:https://stackoverflow.com/questions/41888122/sys-stdout-flush-not-work-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!