Answer the question
In order to leave comments, you need to log in
How to create more than two threads without stopping the program [Python]?
I have a game code for a telegram bot, a timer of 60 seconds is created, for one room it is created normally and works, but when two rooms are created, two threads start working, the timer continues, but the bot stops and the bot does not respond to user messages. I want to fix it so that the timer runs independently, so that you can do more than 2 non-intersecting threads (Sorry if I don’t say so, I’m almost 0 in threads)
g_time = 0
pl_room_id = room_num
ng_threadadd(pl_room_id, g_time, host_id, player_id)
def ng_threadadd(pl_room_id, g_time, host_id, player_id):
event = threading.Event()
thread = threading.Thread(target=ng_everysectimer, args=(event,pl_room_id,g_time,host_id,player_id,))
thread.start()
while not event.isSet():
try:
logging.debug("Добавление из главного потока")
event.wait(0.75)
thread.join()
except KeyboardInterrupt:
event.set()
break
def ng_everysectimer(event, pl_room_id, g_time, host_id, player_id):
while not event.isSet():
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
cursor.execute(f'SELECT game_ended FROM users WHERE user_id = {pl_room_id};')
boolgameend = cursor.fetchone()[0]
if str(pl_room_id) in games and host_id != 0 and player_id != 0 and boolgameend == 0:
if not g_time >= 60:
event.wait(1)
g_time = g_time + 1
else:
gameended(host_id, player_id, pl_room_id)
event.set()
break
elif boolgameend == 1:
event.set()
break
else:
event.set()
break
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question