Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
If the usual sleep does not fit, then use the asynchronous one
https://realpython.com/lessons/asynciosleep-and-wr...
You can use this solution:
from threading import Thread
from collections import namedtuple
import time
import typing
import random
Message = namedtuple('Message', ['delete_time', 'message_id'])
messages_to_delete: typing.List[Message] = []
def delete_message(message: Message):
print(f'Сообщение {message.message_id=} удалено')
messages_to_delete.remove(message)
def messages_deleter():
while True:
for message in messages_to_delete:
if message.delete_time - time.time() <= 0:
delete_message(message)
time.sleep(1)
if __name__ == '__main__':
thread = Thread(target=messages_deleter)
thread.start()
while True:
delete_time = time.time() + random.randint(1, 5)
messages_to_delete.append(
Message(delete_time=delete_time, message_id=random.randint(1, 1000))
)
time.sleep(1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question