Do python sockets stop recv from hanging?

I am trying to create a dual user game in pygame using sockets. The thing is, when I try to get the data in this line:

message = self.conn.recv(1024) 

python hangs until it receives some data. The problem is that this pauses the game loop when the client does not send anything through the socket and causes a black screen. How can I stop recv from doing this?

Thanks in advance

+7
python sockets pygame
source share
1 answer

Use non-blocking mode. (See socket.setblocking .)

Or check if there is data before calling recv . For example, using select.select :

 r, _, _ = select.select([self.conn], [], []) if r: # ready to receive message = self.conn.recv(1024) 
+15
source share

All Articles