0
我定義的模塊級這樣SQLAlchemy的會議在多線程應用程序
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('cx_oracle://username:[email protected]')
Session = sessionmaker(bind=engine)
... data model located on engine
我的數據模型,然後我開始有些線程這樣
threads = []
# start threads
for i in range(4):
# each thread needs it own session
thread = threading.Thread(target=fn, args=(in,))
thread.start()
threads.append(thread)
# wait for threads to finish
for thread in threads:
thread.join()
其中fn(in)
處理一些方法,我使用session = Session()
,然後用於各種目的。
有了這個,我得到各種錯誤,這種錯誤有時只發生。它可以是
DatabaseError: (cx_Oracle.DatabaseError) DPI-1002: invalid OCI handle
DBAPIError: (sqlalchemy.exc.ResourceClosedError) This Connection is closed
InvalidRequestError: Session is already flushing
我真的不知道如何解決,雖然
請提供[mcve],特別是'fn'的內容。 – univerio