首页 > 解决方案 > 为什么调用apscheduler后必须提交select操作,或者阻塞后面的修改?

问题描述

import mysql.connector
from apscheduler.schedulers.background import BackgroundScheduler

def progress_bar():
    pass

def test():
    conn = mysql.connector.connect(user='root', password='123456', database='test', auth_plugin='mysql_native_password')
    cursor = conn.cursor()

    cursor.execute('drop table if exists main')
    conn.commit()
    cursor.execute('create table main (mid int)')
    conn.commit()
    cursor.execute('insert into main values (1)')
    conn.commit()

    sched = BackgroundScheduler()
    job = sched.add_job(progress_bar, 'interval', seconds=1)

    cursor.execute('select * from main')
    print(cursor.fetchall())
    #conn.commit()


test()

在运行代码之前,应在 mysql 中创建一个空模式“test”

我在 jupyter notebook 上运行了两次代码,第一次的 select 已经返回结果但仍在运行,所以它阻止了第二次的 drop 操作

经过大量尝试,代码在这些情况下可以正常运行:

  1. 删除apscheduler的2调用
  2. 不要使用函数'test',直接运行'test'中的行
  3. 在 'select * from main' 的 fetchall 之后添加 'conn.commit()'
  4. 在我崇高的文字上运行

标签: pythonmysqlselectjupyter-notebookcommit

解决方案


推荐阅读