在线程中创建的Phython Flask SQLite对象只能在同一个线程中使用

时间:2016-03-03 08:54:34

标签: python multithreading flask sqlite

我在同一文件夹中的 helloflask.py abcd.py 中有烧录代码:

import abcd as abc
    @app.route("/ddata")
    def stuff1():

        p=abc.run('somefile.txt')
        return json.dumps(p)

abcd.py 有以下代码

connection = sqlite3.connect('C:\\Users\me\Desktop\Python\my.db')
cursor=connection.cursor()

def dbcall(ip_rel,c_mac):
    sql="select count(*) from myDB"
    cursor.execute(sql)
    row=cursor.fetchone()
    count =row[0]

    return count #I am returning a list here in my original code

当我运行helloflask.py时,我在检索行时遇到以下错误:

ERROR:

  

文件“C:\ Users \ mvahuje \ Desktop \ Python \ Application \ Dsharkclass.py”,   第25行,在dbcall cursor.execute(sql)中编程错误:SQLite   在线程中创建的对象只能用于那个线程   object是在线程ID 15108中创建的,这是线程ID 14488

这与线程权限sqlite3有关。但我不确定我还能做些什么让它发挥作用。

单独调出abcd.py在调用其他代码时效果非常好。

0 个答案:

没有答案