Python SQLAlchemy自动加载线程安全吗?
自动加载线程安全吗?我找不到关于这个的任何文件。 根据下面的代码,表无法在大约20毫秒内成功加载(无异常、无警告或更高的日志,但table.columns是一个空列表),然后才能正常工作Python SQLAlchemy自动加载线程安全吗?,python,sqlalchemy,Python,Sqlalchemy,自动加载线程安全吗?我找不到关于这个的任何文件。 根据下面的代码,表无法在大约20毫秒内成功加载(无异常、无警告或更高的日志,但table.columns是一个空列表),然后才能正常工作 from sqlalchemy import * import logging import threading import time logging.basicConfig(level = logging.INFO, format = '%(asctime)s:%(message)s', filename
from sqlalchemy import *
import logging
import threading
import time
logging.basicConfig(level = logging.INFO, format = '%(asctime)s:%(message)s', filename = "autoload_test.log")
logging.getLogger("sqlalchemy").setLevel(logging.DEBUG)
engine = create_engine("postgresql://madk@localhost/mydb", echo = False)
metadata = MetaData(bind=engine)
lock = threading.Lock()
def load_table():
while True:
try:
table = Table('mytbl', metadata, autoload=True, autoload_with=engine)
logging.info("size of columns:[%s]", len(table.columns))
except:
logging.warn("error occured", exc_info = True)
for i in range(5):
t = threading.Thread(target = load_table)
t.daemon = False
t.start()
time.sleep(4)
尝试关闭
t.daemon=False
或添加尝试。。。例外情况除外,如e:logger.Exception(e)
,以获取实际的错误消息。现在你看不到它,问题中就没有了它。守护进程线程悄悄地吃掉异常。@Mikkoohtama谢谢,根据您的建议进行了更改,得到了相同的结果:没有异常,没有警告或更高级别的日志,在多线程下,只有空列列表,没有锁保护。我刚刚遇到这个问题,注意到文档说元数据对于读取操作是线程安全的,但不一定是写入操作。感谢您的锁定建议,操作文档: