0

The important part of the code is the first several lines. I want to know how to return after a certain amount of time. I know Timer calls a function after some time but I dont know how to return with it.

def rdt_rcv(conn, flag):
    t2 = threading.Timer(5.0, rdt_rcv, return, [0, 0, 0, 0])
    t2.start()
    # if timeout, return
    #return None, None, None, None
    field1 = conn.recv(BUFFER_SIZE)
    field1 = int(field1)
    field2 = conn.recv(BUFFER_SIZE)
    field2 = int(field2)
    if field2 != 0:
        global ACK_received
        ACK_received = 1
    field3 = (conn.recv(BUFFER_SIZE))
    field3 = int(field3)
    field4 = (conn.recv(BUFFER_SIZE))
    field4 = int(field4)
    # stop timer
    t2.cancel()
    return field1, field2, field3, field4
  • This is just a special case duplicate of [timeout-on-a-function-call](http://stackoverflow.com/questions/492519/timeout-on-a-function-call) question. – matusko Apr 16 '17 at 23:36

1 Answers1

0

change.

# stop timer
t2.cancel()
return field1, field2, field3, field4. 

to.

if t2.is_alive():
    # stop timer
    t2.cancel()
    return field1, field2, field3, field4. 
else:
    return 0, 0, 0, 0
stovfl
  • 14,998
  • 7
  • 24
  • 51