It seems that there are threads shown as still active in Visual Studio Code which should already be ended.
I am new to Python and maybe I am understanding something not right how the threading works there.
My example runs a TCP socket server which opens a new thread for each client that connects.
The thread should stay alive until the connection to the client gets closed.
This is my sample code.
import socket import os from _thread import * ServerSideSocket = socket.socket() host = '127.0.0.1' port = 2004 ThreadCount = 0 try: ServerSideSocket.bind((host, port)) except socket.error as e: print(str(e)) print('Socket is listening..') ServerSideSocket.listen(5) def multi_threaded_client(connection, address): connection.send(str.encode('Server is working:')) while True: data = connection.recv(2048) # abort while loop if connection is closed if not data: break print('Data from: ' + address + ':' + str(address)) print (data) response = 'Server message: ' + data.decode('utf-8') + '\n' connection.sendall(str.encode(response)) connection.close() print('Connection closed: ' + address + ':' + str(address)) # thread should end there from my understanding while True: Client, address = ServerSideSocket.accept() print('Connected to: ' + address + ':' + str(address)) start_new_thread(multi_threaded_client, (Client, address, )) ThreadCount += 1 print('Thread Number: ' + str(ThreadCount)) ServerSideSocket.close()
The code seems to work in general, but Visual Studio Code shows me that there are threads still running which actually should be already terminated.
I would asume that “Dummy-7” is already terminated and it should not apear as running thread in the call stack window.
Is this the way how it should be?
Is “Dummy-7” really still running at this moment? If yes, how to terminate it correctly?