I have a class that subclasses a Thread, and I'm having trouble creating objects with mySQL connections inside it asynchronously. Each MyObject
has a mysql connection pool that is created on __init__
. I'm running into an issue where creating MyObject
causes blocking, despite supposedly being asynchronous. Is there something in mysql-connector that blocks concurrent execution, and how can I work around this?
Before adding threading, here's what MyObject
creation looked like:
self.objects = [MyObject(*o, self) for o in objectData]
But delays would accumulate as more instances of MyObject
were created
So I tried to creating MyObject
with threads:
self.objects = []
threads = [
threading.Thread(
target=lambda o: self.objects.append(MyObject(*o, self)),
args=(o,)
) for o in objectData
]
for t in threads:
t.start()
for t in threads:
t.join()
Even though object instances are created immediately, it still takes an increasing amount of time to create a connection pool in MyObject
. Is there something in mysql-connector that blocks concurrent execution, and how can I work around this?
Edit:
This is the code for MyObject
:
class MyObject(Thread):
def __init__(self, *args, manager):
self.parent = manager
self.cnxPool = mysql.connector.pooling.MySQLConnectionPool(
pool_name=f"{get_ident()}",
pool_size=10,
host=HOST,
user=USER,
password=PASSWORD,
port=25060
)
self.stopEvent = Event()
Thread.__init__(self)
self.daemon = True
def run(self):
...
objectData
is loaded from json, there are a variable amount of args