M
M
MichaelMih2021-10-21 13:25:11
Multithreading
MichaelMih, 2021-10-21 13:25:11

What to do if the script terminates after the execution of the loop in the created thread?

Hello! There is a file ( events.py ) in which we process incoming sockets from the site.
It has a cycle, after the function, self.db.auction_end()the site script completely stops working. Also, the self.db.auction_end() function is not executed to the end ( after print(5) there is no print(6) ).

def auction_end()

print(1)
        sql = "SELECT * FROM auction ORDER BY summ DESC"
        self.cur.execute(sql)
        resp = self.cur.fetchall()
        print(2)
        user = self.get_row("users", "*", f"id=%s", resp[0]['user_id'])[0]
        if len(resp) == 0:
            print(" NUUUUUUUUUUUUUUUUUUUUUUUUUUUUULLLLL")
            return

        summ = 0
        for i in resp:
               summ += int(i['summ'])
        resp = resp[0]
        summ = summ - summ / 100 * 20
        win_summ = int(summ)
        summ = summ + int(user['score'])
        summ = int(summ)
        print(3)
        self.update_score(summ, resp['user_id'], 2, "Победа в аукционе")
        print(4)
        self.cur.execute("DELETE FROM auction")
        print(5)
        self.conn.commit() 
        print(6)
        last_win = self.get_row("last_winners")
        time = datetime.now().strftime("%d.%m.%Y %H:%M:%S")
        print(len(last_win))
        if len(last_win) < 3:
            print("y")
            self.add_row("last_winners", login=user['login'], summ=win_summ, time=time, img=user['image'])
        elif len(last_win) == 3:
            sql = """DELETE FROM last_winners WHERE id in(SELECT MIN(id) FROM last_winners) """
            self.cur.execute(sql)
            self.conn.commit()
            self.add_row("last_winners", login=user['login'], summ=win_summ, time=time, img=user['image'])


cycle
class Auction(Thread):
    db = DataBase()
    def __init__(self) -> None:
        Thread.__init__(self)
    
    def run(self):
        while True:
            sleep(3)
            timer = self.db.get_row("timer")[0]
            time = datetime.strptime(timer['start_time'], "%d.%m.%Y %H:%M:%S")
            time_1 = time - timedelta( seconds=60)
            time = time + timedelta(minutes=3, seconds=30)
            time_2 = datetime.now()
            if timer['active'] == 1:
                if time_1 <= time_2:
                    print("end")
                    self.db.update_row("timer", active=0)
                    self.db.auction_end()
                    continue

            if time <= time_2 and timer['active'] == 0:
                respones = {"name": "Bot", "summ": 50}
                socketio.emit("add_bet", respones, namespace='/app/auction', room="1")
                now = datetime.now().strftime("%d.%m.%Y %H:%M:%S")
                self.db.update_row("timer", start_time=now, active=1)    
                self.db.auction_update(24, 50)
                continue
            continue
            


t = Auction()
t.start()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question