修改Jupyter配置后仍触发IOPub数据速率超限问题求助
问题
运行以下数据库查询代码时触发了IOPub data rate exceeded错误:
%%time try: # get the correct ids Session = sessionmaker(bind=cnx) # Create a Session session = Session() # Reflect the database to get the table metadata metadata = MetaData() patients_table = Table('ekg_patient_tracings', metadata, autoload_with=cnx) # Prepare the query query = select(patients_table).where(patients_table.c.patient_hash.in_(patient_hashes)) # Execute the query result = session.execute(query) # Fetch all rows rows = result.fetchall() df_ekg_ids = pd.DataFrame(rows) except Exception as e: print("An error occurred:", e) finally: # Close the session session.close()
已修改/home/user/.jupyter/jupyter_notebook_config.json配置文件,将NotebookApp和ServerApp的iopub_data_rate_limit设为10000000000.0、rate_limit_window设为3.0,但重启内核后仍出现相同错误,速率限制未生效。
相关版本信息:
Selected Jupyter core packages... IPython : 8.25.0 ipykernel : 6.23.1 ipywidgets : 8.1.3 jupyter_client : 8.2.0 jupyter_core : 5.3.0 jupyter_server : 2.5.0 jupyterlab : 4.2.3 nbclient : 0.7.4 nbconvert : 7.4.0 nbformat : 5.8.0 notebook : 6.4.12 qtconsole : 5.5.2 traitlets : 5.14.3
排查与解决办法
配置不生效的原因
- 配置文件优先级问题:Jupyter Notebook 6.x版本优先读取
jupyter_notebook_config.py而非JSON配置文件,若同时存在py格式配置,JSON里的设置会被覆盖。 - 配置项适用版本不匹配:
ServerApp是Jupyter Server(对应Notebook 7+或JupyterLab)的配置项,当前使用的Notebook 6.4.12核心配置项仍属于NotebookApp,ServerApp的设置对它不生效。 - 未彻底重启服务:仅重启内核无法加载新配置,需要彻底关闭Jupyter Notebook进程后重新启动。
具体解决步骤
方法一:修正配置文件(推荐)
- 检查是否存在
/home/user/.jupyter/jupyter_notebook_config.py,若存在直接编辑;若不存在,生成配置文件:jupyter notebook --generate-config - 在py配置文件中添加或修改以下内容:
c.NotebookApp.iopub_data_rate_limit = 10000000000.0 c.NotebookApp.rate_limit_window = 3.0 - 保存文件后,彻底关闭所有Jupyter Notebook进程,再重新启动服务。
方法二:启动时临时指定参数
如果不想修改配置文件,可在启动Jupyter Notebook时直接传入参数:
jupyter notebook --NotebookApp.iopub_data_rate_limit=10000000000.0 --NotebookApp.rate_limit_window=3.0
方法三:优化代码避免触发限制(根本解决)
放宽限制只是临时方案,更合理的方式是优化查询逻辑,减少一次性输出的数据量:
- 分批读取数据,替换
fetchall():# 替换rows = result.fetchall() batch_size = 1000 rows = [] while True: batch = result.fetchmany(batch_size) if not batch: break rows.extend(batch) df_ekg_ids = pd.DataFrame(rows) - 抑制大DataFrame自动打印,在代码末尾加
;:df_ekg_ids = pd.DataFrame(rows);
内容的提问来源于stack exchange,提问作者Ruby Midford
相关产品推荐
相关产品推荐

