F
F
FOUREX2021-11-02 17:13:06
Python
FOUREX, 2021-11-02 17:13:06

How to make sure that the Socket does not stop the loop?

import socket

while True:
  data = connection.recvfrom(1024)
  players = literal_eval(data[0].decode("utf8"))


  pygame.draw.circle(sc, (255, 255, 255), (players), 6)


I have in the cycle
data = connection.recvfrom(1024)
, and until the server sends a packet, the loop stays in place. How can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-02
@Vindicar

There are different ways. It is possible to receive data in a separate thread, but this brings its own problems, and quite a lot.
I would suggest learning how to use select() - this function allows you to find out if there is unreceived data on the socket before trying to accept it.
You can also try to learn how to work through asyncio, this makes it easier to improve the application in the future. But it's also more difficult for a beginner, it takes a little "dislocate the brain" to understand how an asynchronous program works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question