How to set timeout on python's socket recv method? -


i need set timeout on python's socket recv method. how it?

the typical approach use select() wait until data available or until timeout occurs. call recv() when data available. safe, set socket non-blocking mode guarantee recv() never block indefinitely. select() can used wait on more 1 socket @ time.

import select  mysocket.setblocking(0)  ready = select.select([mysocket], [], [], timeout_in_seconds) if ready[0]:     data = mysocket.recv(4096) 

if have lot of open file descriptors, poll() more efficient alternative select().

another option set timeout operations on socket using socket.settimeout(), see you've explicitly rejected solution in answer.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -