Answer the question
In order to leave comments, you need to log in
How to delete the thread object when it ends?
Good day!
I have a SocketServer class. In it, I run several threads for rooms:
def __init__(self, socket_io_p, start_list_events):
"""
Инициализатор класса.
:param socket_io_p: Серверный сокет для общения с клиентами.
:param start_list_events: Начальный набор событий.
"""
# Наш серверный сокет.
self.__socket_io = socket_io_p
# Запускаем обработчики.
self.client_join_room = self.__socket_io.on("join")(self.__client_join_room)
self.client_leave_room = self.__socket_io.on("leave")(self.__client_leave_room)
# Начальный набор трансляций и список комнат.
# По ходу работы будет изменяться в размерах из-за добавления
# или завершения трансляций.
self.__event_threads = []
self.__list_rooms = []
for i, event in enumerate(start_list_events):
self.__list_rooms.append(event.room)
self.__event_threads.append(Thread(target=self.__room_thread,
args=(event,)))
self.__event_threads[i].start()
print(f"{event.room} started")
with Lock():
# По окончании трансляции удаляем комнату и поток.
for i, room in enumerate(self.__list_rooms):
if room == current_room:
self.__event_threads.pop(i)
self.__list_rooms.pop(i)
print(f"{room} deleted")
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