i'm trying to write a socket-server program to send keyboard inputs from my PC to my Raspberry Pi, but when i create a new thread for a function, that has a while loop in it, the script execution does not continue as expected, instead it is stuck. These are my scripts:
import socket
import threading
class Server:
def __init__(self, host: str, port: int):
self.host = host
self.port = port
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((host, port))
self.server.listen()
self.client = None
self.message = ""
def stream(self):
while True:
self.client.send(self.message.encode('utf-8'))
def run(self):
while True:
print('Server is running and listening ...')
self.client, address = self.server.accept()
print(f'Connection is established with {str(address)}')
thread = threading.Thread(target=self.stream)
thread.start()
from server import Server
import threading
server = Server('192.168.178.30', 8001)
thread = threading.Thread(target=server.run)
thread.start()
while True:
try:
print("A")
except KeyboardInterrupt:
break
When i executed the second program it didn't output A's, instead only executed, what's in the while loop of the run function of the first script.