Home > Mobile >  I'm using python threading and when querying mysql the code seems to run and stays in an endles
I'm using python threading and when querying mysql the code seems to run and stays in an endles

Time:01-29

I'm calling two functions at the same time and the functions are triggered, but when the mysql query is made, the code seems to run and stays in an infinite loop, not returning an error.

here is my code:

main.py

import threading
import siparis_kontrolu
import dolar_euro_guncelle

def dd1():
    print("a")
    siparis_kontrolu.siparis_kontrolu()


def dd2():
    print("b")
    dolar_euro_guncelle.dolar_euro_guncelle()

t1 = threading.Thread(target=dd1)
t2 = threading.Thread(target=dd2)

t1.start()
t2.start()

siparis_kontrolu.py

import mysql_db
def siparis_kontrolu():
    try:
        while True:
            print("test1")
            tum_kullanicilar = mysql_db.mysql_get_all("select * from users")
            print("test2")

dolar_euro_guncelle.py

import urllib.request
import json
import mysql_db
import time
import logla


def dolar_euro_guncelle():
    while True:
        try:
            print("f")
            data = urllib.request.urlopen(
                "https://finans.truncgil.com/today.json")
            for line in data:
                line = json.loads(line.decode('utf-8'))
                USD = round(float(line['USD']["Satış"].replace(",", ".")), 2)
                EUR = round(float(line['EUR']["Satış"].replace(",", ".")), 2)
                mysql_db.mysql_update("update variables set EUR='"  
                                      str(EUR) "', USD='" str(USD) "' where id='4'")
            time.sleep(10)
            print("USD/EUR guncellendi.")
        except Exception as e:
            logla.logla(e, "dolar_euro_guncelle")
            print(e)

mysql_db.py

from configparser import ConfigParser
import mysql.connector

config_file = 'config.ini'
config = ConfigParser()
config.read(config_file)

mydb = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
                               user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor = mydb.cursor(buffered=True, dictionary=True)

def mysql_update(sorgu):
    try:
        mycursor.execute(sorgu)
        mydb.commit()
    except Exception as e:
        print(e)

def mysql_get_all(sorgu):
    try:
        mycursor.execute(sorgu)
        return mycursor.fetchall()
    except Exception as e:
        print(e)

When I run main.py these are written to the console:

a

b

f

test1

test2 and USD/EUR guncellendi is not printed, I don't understand exactly what's wrong, when I trigger siparis_kontrolu.py directly without using threading, it works fine

It remains as it appears in the picture and the code does not stop. But it doesn't do what I want it to do.

enter image description here

CodePudding user response:

It looks like a concurrency issue with the DB connection (assuming your siparis_kontrolu.py doesn't throw syntax errors due to the missing except / finally part of the try statement, a sleep is also missing).

The "SELECT" statements conflict with the "Update" statements.

So it turns out: each thread needs its own database connection!

The easiest way is to open another database connection and create another cursor (maybe another cursor instance is enough) in your mysql_db.py:

from configparser import ConfigParser
import mysql.connector

config_file = 'config.ini'
config = ConfigParser()
config.read(config_file)

mydb = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
                               user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor = mydb.cursor(buffered=True, dictionary=True)

mydb_upd = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
                               user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor_upd = mydb_upd.cursor(buffered=True, dictionary=True)

def mysql_update(sorgu):
    try:
        mycursor_upd.execute(sorgu)
        mydb_upd.commit()
    except Exception as e:
        print(e)

def mysql_get_all(sorgu):
    try:
        mycursor.execute(sorgu)
        return mycursor.fetchall()
    except Exception as e:
        print(e)

But creating a DbHandler class and use instances of it in the several threads would be much cleaner.

  • Related