Hi,
I am trying to perform some database actions after multiprocessing some
database manipulations but in most cases I get:
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError)
(2006, 'MySQL server has gone away')
The code below is a working example of the problem I'm having, where the
success of line 3 (in main) for some reason depends on what happens on line
1.
Any help would be appreciated!
Regards,
Geert Jan
from sqlalchemy import create_engine, select
from sqlalchemy import Column, Integer
from sqlalchemy.orm import declarative_base, sessionmaker
import multiprocessing as mp
import numpy as np
Base = declarative_base()
Session = sessionmaker()
class MyTable(Base):
"""This table lists all observed frames."""
__tablename__ = 'mytable'
# Columns in the table.
id = Column(Integer, primary_key=True, autoincrement=False)
def insert_integer(integer):
with Session() as session:
record = MyTable(id=int(integer))
session.add(record)
session.commit()
return
def query_integer(integer):
with Session() as session:
statement = select(MyTable).where(MyTable.id == int(integer))
result = session.execute(statement).scalar_one_or_none()
return result
def database_action(integer):
result = query_integer(integer)
if result is None:
insert_integer(integer)
else:
print("integer already in database.")
return
def initializer(engine):
# Doesn't matter if I use on or the other.
engine.dispose(close=False)
# engine.pool = engine.pool.recreate()
def main():
# Database setup.
engine = create_engine("mysql+mysqldb://user:[email protected]/testdb")
Session.configure(bind=engine)
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
# Line 1.
# If nothing happens on line 1, line 3 fails.
# insert_integer(0) # If I do this (insert-only) line 3 fails.
database_action(0) # If I do this (query, insert) line 3 works.
# Line 2.
with mp.Pool(2, initializer, (engine,)) as pool:
pool.map(database_action, np.arange(1, 11))
# Line 3.
database_action(20) # Fails if line 1 is insert_integer.
return
if __name__ == '__main__':
main()
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/5597b069-3725-44de-88c4-4964d95d84cen%40googlegroups.com.