Python 3.x 如何在数据库中插入/更新日期?我使用了Python3、SQLAlchemy和多处理。连接不可用
我的Python模块用于多处理 但我的模块正在打印错误消息: OperationalError(“MySQL连接不可用”)OperationalError: MySQL连接不可用 无论DB的类型如何,都会出现类似的现象 并且只执行第一次更新或插入,之后不操作Python 3.x 如何在数据库中插入/更新日期?我使用了Python3、SQLAlchemy和多处理。连接不可用,python-3.x,sqlalchemy,connection,multiprocessing,Python 3.x,Sqlalchemy,Connection,Multiprocessing,我的Python模块用于多处理 但我的模块正在打印错误消息: OperationalError(“MySQL连接不可用”)OperationalError: MySQL连接不可用 无论DB的类型如何,都会出现类似的现象 并且只执行第一次更新或插入,之后不操作 utils/database.py class DataBase: engine = create_engine('db connect info') db_session = scoped_session(session
- utils/database.py
class DataBase: engine = create_engine('db connect info') db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base = declarative_base() Base.query = db_session.query_property() conn = engine.connect() def init_db(self): import models self.Base.metadata.create_all(self.engine)
- 检查\u id\u module.py
from multiprocessing import Process, Queue ... def update_status(checkpoint2_list): DB.db_session.query(Mymodel). \ filter(Mymodel.username.in_(checkpoint2_list)). \ update({Mymodel.status: 'test'}, synchronize_session=False) DB.db_session.commit() DB.db_session.close() def check_user(queue): checkpoint2_list = [] while True: if queue.empty(): break username = queue.get() api_url = 'request url'.format(username) res = requests.get(api_url, headers=headers) if res.status_code == 404: print(username, res.status_code) checkpoint2_list.append(username) if checkpoint2_list: update_status(checkpoint2_list) if __name__ == '__main__': procs = [] starttime = datetime.now() accounts_queue = Queue() accounts = DB.db_session.query(Mymodel.username).filter( Mymodel.status.in_(['run', 'run2']) ) insert_queue(accounts_queue, accounts) for i in range(20): proc = Process(target=check_user, args=(accounts_queue,)) procs.append(proc) proc.start() for proc in procs: proc.join() accounts_queue.close() endtime = datetime.now() print('\nRunning Time : ', endtime - starttime)
在我的源代码中,哪里发生了多处理连接错误?您是否检查了mysql打开的连接数?所有与连接相关的错误都会发生,而与数据库无关,包括mysql、postgres、sqlite和sqlite。