首页 > 解决方案 > 如何在 Jupyter Notebook 中强制执行代码执行顺序?

问题描述

观察到的行为

在已展示的 Jupyter Notebook 中,该笔记本产生以下输出:

ERP at 175 ms of trial 1
Average ERP at 175 ms of all trials

后附2张图:

<display image 0>
<display image 1>

当执行以下代码时:

z = sX_train[1,1,:,int(0.175*40)]
c="channels.txt"
print("ERP at 175 ms of trial 1")
topoplot(z,c)

# also compute the average ERP of all trials in first epoch at 180 ms
z = np.zeros(64)
for channel in range(0,z.shape[0]):
    z[channel] = np.mean(sX_train[:,1,channel,int(0.175*40)])
print("Average ERP at 175 ms of all trials")
topoplot(z,c)

预期行为

但是,我希望输出为:

ERP at 175 ms of trial 1
<display image 0>
Average ERP at 175 ms of all trials
<display image 1>

如果要从上到下执行代码。

分析

我认为这是一些异步函数,因为计算topoplot函数比计算打印语句需要更长的时间。但是我在线程或同步方面找不到任何东西。一个快速而肮脏的解决方案是修改拓扑图以返回一些东西,以便代码在打印描述之前必须等待它。

问题

如何确保输出按照在 Jupyter Notebook 的单元格中写入的顺序显示,而不修改代码以建立相互依赖关系?

标签: pythonjupyter-notebook

解决方案


推荐阅读