jupyter输入,显示,打印执行顺序混乱 [英] jupyter input,display, print excute order is chaotic
问题描述
我正在使用jupyter,而我的python版本是3.5.在while循环中,执行顺序不正确,输入语句在print语句之前.这是我的代码.
I'am using jupyter and my python version is 3.5. In a while loop, excute order is not correct, input statement is before print statement. This is my code.
from IPython.display import display
import pandas as pd
df = pd.DataFrame({'a':[1,2],'b':[3,4]})
while(True):
a = input("please input:\n")
display(df.head())
print (a)
执行结果是
推荐答案
我能够在OSX的Chrome 63下重现该行为.我添加了多个连续的print(a)
语句,并且输入字段结尾的位置是随机的:在它们之前,之后或之间.我怀疑每个显示和打印调用都会向服务器发送一个请求,但会异步等待其结果,因此在print(a)
的结果准备好之前,可以再次调用input
.
I was able to reproduce the behavior under Chrome 63 on OSX. I added several more consecutive print(a)
statements and where the input field ends up is random: before them, after them, or in between. My suspicion is that each display and print call sends a request to the server but awaits its result asynchronously, so that input
may be called again before the result from print(a)
is ready.
这不是一个很好的解决方案,但是在print(a)
之后添加一小段睡眠(time.sleep(.02)
)可以解决我的问题.
It is not an elegant solution, but adding a small sleep (time.sleep(.02)
) after print(a)
fixes the problem for me.
这篇关于jupyter输入,显示,打印执行顺序混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!