Streamlit-在线程中创建的SQLite对象只能在同一线程中使用

8位博尔赫斯

我将Streamlit用作数据处理和可视化框架。

遵循有关使用Streamlit进行高级缓存的文档,以及如何创建数据库连接以及如何将它们传递给其他功能,我正在使用sqlite3并尝试在运行时创建一个连接,并通过以下操作传递“游标”:

import streamlit as st

# create database connection
@st.cache(allow_output_mutation=True)
def get_database_connection():
    conn = sqlite3.connect('collect/Data/fpl.db')
    c = conn.cursor()   
    return c

在这里,我尝试通过传递我的连接ID hash_funcs,返回创建的游标:

# pass connections around
@st.cache(allow_output_mutation=True, hash_funcs={sqlite3.Cursor:id})
def get_teams_basic(c):
    df_teams = sql('SELECT * FROM TeamsBasic', c)
    return df_teams

@st.cache(allow_output_mutation=True, hash_funcs={sqlite3.Cursor:id})
def get_players_basic(c):   
    df_player_basic = sql('SELECT * FROM PlayersBasic', c)
    return df_player_basic

# and so on...

这是我的sql()

def sql(query, cursor):
    '''
    Takes an SQL query string, and outputs a
    dataframe representation of the query result.
    '''
    # Execute the sql query
    cursor.execute(query)

    # Get the query into a dataframe and set columns
    df_temp = pd.DataFrame(cursor.fetchall())
    df_temp.columns = [x[0] for x in cursor.description]

    # Set the sql id as the dataframe index
    index_column = df_temp.columns[0]
    df_temp.set_index(index_column, drop=True, inplace=True)

    return df_temp

然后,在处main(),实例化数据库连接,并将光标传递给我的函数:

def main():
    c = get_database_connection()
    teams(c)
    players(c)

if __name__ == '__main__':
  main()

但是当代码到达第二个连接时,players()出现以下错误:

ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 123145444151296 and this is thread id 123145454661632.

追溯:

File "/Users/me/anaconda2/envs/data_science/lib/python3.7/site-packages/streamlit/ScriptRunner.py", line 322, in _run_script
    exec(code, module.__dict__)
File "/Volumes/Dados/Documents/Code/Apps/app.py", line 280, in <module>
    main()
File "/Volumes/Dados/Documents/Code/Apps/app.py", line 273, in main
    managers(c)
File "/Volumes/Dados/Documents/Code/Apps/app.py", line 149, in managers
    df_players = get_players_basic(c)
File "/Users/me/anaconda2/envs/data_science/lib/python3.7/site-packages/streamlit/caching.py", line 594, in wrapped_func
    return get_or_create_cached_value()
File "/Users/me/anaconda2/envs/data_science/lib/python3.7/site-packages/streamlit/caching.py", line 578, in get_or_create_cached_value
    return_value = func(*args, **kwargs)
File "/Volumes/Dados/Documents/Code/Apps/app.py", line 64, in get_players_basic
    df_player_basic = sql('SELECT * FROM PlayersBasic', c)
File "/Volumes/Dados/Documents/Code/Apps/collect/PythonFunctions/sqlfunctions.py", line 111, in sql
    cursor.execute(query)

我想念什么?

sk

我不确定您是否仍在解决此问题,但是如果您是我,发现关于此主题的讨论线程可能有用。我将很快在我的一个项目中进行尝试。

https://discuss.streamlit.io/t/prediction-analysis-and-creating-a-database/3504

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ProgrammingError:在线程中创建的SQLite对象只能在同一线程中使用

从Flask使用SQLAlchemy会话会引发“在线程中创建的SQLite对象只能在同一线程中使用”

SQLAlchemy + pyTelegramBotAPI:在线程中创建的SQLite对象只能在同一线程中使用

在线程A中创建对象,在线程B中使用。是否需要互斥体?

在Streamlit中如何使用图表达在Y轴上绘制多个值(相同单位)?

在线程中创建的DatabaseWrapper对象只能在同一线程中使用。”当尝试使用celery插入数据库时

python streamlit运行问题

如何在Python脚本中运行Streamlit应用程序?

隐藏底部的HTML /小部件,并从Streamlit file_uploader()中删除拖放

如何在streamlit中显示热图颜色相关图

无法在命令提示符下使用 pip 下载 streamlit

使用 pip 安装 streamlit 时出错

如何通过允许用户使用 plotly 和 python with streamlit 选择列来绘制条形图

如何在python中使用pandas和streamlit替换数据帧上的值?

Streamlit 中的下拉菜单没有括号和引号

UFW + Nginx + Lets Encrpyt + Streamlit 的正确使用流程是什么

使用 Streamlit + Altair 绘制多系列数据框

如何修复python和streamlit中的错误“DataFrame对象不可调用”

如何使用python在streamlit应用程序中打开atoti链接?

如何修改streamlit中的選擇框列長度?

允許用戶從 streamlit 的下拉列表中選擇 NULL

運行streamlit時如何在地址欄中顯示URL?

當我使用 st.cache 時,Streamlit Unhashable TypeError

如何在streamlit markdown中使用python变量

Onnx 推理在 streamlit 框架内使用 numpy float32 数据类型引发错误

如何在 Streamlit 中使用多选动态绘制比较?

将 Streamlit 中的数据转换为 CSV 文件

在 Streamlit 中维护数据框的显示

Streamlit Python - 每次换行