0

Is there any module that exists that will stop the execution of a specific value in a loop if the time taken to compute reaches more than a specific amount of seconds?

For example, in the code:

for i in range(10):
    print(socket.gethostbyname(entry[i]))

If it takes longer than 1 second to compute socket.gethostbyname(entry[i]), I want it to skip and go to the next one.

Thanks!

  • At a guess (there may be some nicer functionality, possibly in an external package), you'd need to create a thread, put the gethostbyname functionality in that thread, keep a timer in another thread, and when the timer surpasses a set time (one second) and that thread ends, kill the first thread as well. – 9769953 May 12 '22 at 14:46
  • Related question (but not exactly a duplicate): https://stackoverflow.com/questions/2196999/how-to-add-a-timeout-to-a-function-in-python . – 9769953 May 12 '22 at 14:47
  • I think you can call this before your loop https://docs.python.org/3/library/socket.html#socket.setdefaulttimeout – Anentropic May 12 '22 at 15:36

0 Answers0