我的Python模块用于多处理。
但我的模块正在打印错误消息:
OperationalError(“MySQL连接不可用。”)OperationalError: MySQL连接不可用
无论DB的类型如何,都会出现类似现象。
只执行第一次更新或插入,之后不再操作。
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)
check_id_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)
我的源代码中出现了多处理连接错误?