I am creating a client web socket that connects to a server. And I am starting the connection using a Tkinter gui button. To prevent gui from getting stuck I am using threading. My client disconnects from the server after some time. So when I try to connect again using the button it shows an error that a Thread can only be started once.
My code for Button and client method:
async def start_client(url, duration):
async with websockets.connect(url) as ws:
start_time = time.time()
while time.time() - start_time < duration:
message = await ws.recv()
msg = json.loads(message)
print(msg)
if 'timestamp android.sensor.accelerometer' in msg:
write_acc(msg)
else:
write_gyro(msg)
def on_click():
initial_setup_files()
asyncio.run(start_client(url, 4))
final_csv_file_creator()
print("Disconnected from the server")
btn = Button(window,
text="Start Server ",
fg="black",
bg="cyan",
font=("Times New Roman", 30, 'bold'),
command=threading.Thread(target=on_click).start
)
btn.pack(padx=20,
pady=20)
I was expecting that client connects again without errors