Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

576
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda