-1

I am using settimeout() function in such way:

import socket

s.connect((host,port))

s.settimeout(1)   #a
try:
    while True:
        s.settimeout(0.030)    #b
        try:
            code
        except socket.timeout:
            code
            break
except socket.timeout:
    print('over')

my code is in this form. Now timeout at a is not working but timeout at b is working because the code in timeout at b is executing

My project is to run further program for almost 1 sec but it is moving in continuous loop. I know it is because of while True but the code under loop should run continuously for 1 sec

If anyone have another method or tricks are heartily welcome

RamSharma
  • 93
  • 8
54TY4M
  • 61
  • 1
  • 7

1 Answers1

0

If I understand you correctly:

import socket

s.connect((host,port))
s.settimeout(1)
#a
try:
  while True:
      s.settimeout(0.030) 
      #b
      try:
        # code...
      except socket.timeout:
          # code... 
          break
except socket.timeout:
  print('over')

You are trying to limit the time of each request in inside the loop to 0.03 seconds, and the complete process to 1 second. If so, you cannot limit the running time of the whole loop by setting the timeout socket, since it refers to a single socket operation.

You might consider using this solution.

Cider
  • 325
  • 1
  • 13