Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

577
Vistas
Run multiple queries at the same time on Mysql using threading in python

I have multiple queries that I need to execute on MySQL DB using python. If I have 3 queries running 5 mins each then it takes approx total 15 mins to execute all queries

Is it possible to execute/fire all 3 queries at the same time using threading so the executing time is reduced to approx 5 mins?

when I try multi-threading or multi-processing iI get the error as

OperationalError: 2013 (HY000): Lost connection to MySQL server during query

def connect(host,username,passwor,db_name):
    mysql_db = mysql.connector.connect(
          host = host,
          user = username,
          passwd = password,
          database = db_name
            )
    cursor = mysql_db.cursor()
    return(cursor)
        
def execute_query(cursor,query):
    cursor.execute(query)
    res = cursor.fetchall()
    que.put(res)
    
query_1 = "select count(*) from db.table1"
query_2 = "select count(*) from db.table2"
query_3 = "select count(*) from db.table3"

cursor = connect((host,username,passwor,db_name):
threads = []
que = Queue.Queue()

threads.append(Thread(target = execute_query, args = (cursor,query1,)))
threads.append(Thread(target = execute_query, args = (cursor,query2,)))
threads.append(Thread(target = execute_query, args = (cursor,query3,)))

for thread in threads:
    thread.start()
    
for thread in threads:
    threads.join()
    res = que.get()
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda