0
I have in the file Connections.py two functions, one where I am reading the 'status' field of a table every 1 second.
I also have another function in this same file doing the same process, but in another table.
Below, I list the contents of the file mentioned above (Connections.py)
import mysql.connector, time
import _thread as thread
db_connection = mysql.connector.connect(
host="localhost",
user="meuUsername",
passwd="minhaSenha",
database="meuBD"
)
my_database = db_connection.cursor()
def getTable1(): #Lê status de detecção / nome e identify
while True:
time.sleep(1)
sql_statement = 'SELECT peopleId FROM detections where status = 1'
my_database.execute(sql_statement)
output = my_database.fetchall()
print('Executando Thread getTable1()')
def getTable2():#lê status de reconhecimento
while True:
time.sleep(1)
sql_statement = 'SELECT * FROM recognitions where status = 1'
my_database.execute(sql_statement)
output = my_database.fetchall()
print('Executando Thread getTable2()')
In another file, called index.py, I am importing the contents of the file Connections.py and instantiating two threads, one for each reading function in the database.
import threading
import os, connections as cn
if __name__ == "__main__":
processos = []
processos.append(threading.Thread(target=cn.getStatusDetections()))
processos.append(threading.Thread(target=cn.getStatusRecognitions()))
# Ex: adicionar o porcesso `threading.Thread(target=inicia_programa, args=('x.py',))`
for processo in processos:
processo.start()
However, both threads are not executed at the same time and are executed in sequence, which results in never leaving the execution of the getTable1 function().
On the console is printed every second only:
Executando Thread getTable1()
Executando Thread getTable1()
Executando Thread getTable1()
Executando Thread getTable1()
Being that I wish it were shown the result of the two so (no matter if it is in order):
Executando Thread getTable1()
Executando Thread getTable2()
Executando Thread getTable1()
Executando Thread getTable2()
Would someone know to give me some knowledge on how to treat this in a way that works the execution of the two threads simultaneously?
Good boy, that’s just what I needed. I’m glad it was such a silly thing kkkkk.
– João Paulo Silva