1

I have a for loop that is making POST requests. Each one takes around 1-8 minutes to complete and I want my loop to continue and skip over if a certain amount of time goes by, such as 15 minutes.

What would be a good way of accomplishing this? I've looked into potentially using asynchronous functions but I'm not sure how to go about that yet or if it's even the right approach.

its a super simple loop, its basically:

for url in list_of_urls: 
   request = requests.post(url)
   excel_sheet = #write header, status code, etc to document

    #save excel
maxokream
  • 43
  • 5

1 Answers1

0

You can use requests "timeout" argument. It will throw an exception if the request takes longer than this number of seconds, which you can easily catch:

for url in list_of_urls: 
    try:
        request = requests.post(url, timeout=60)
    except requests.exceptions.ReadTimeout:
        print("Timeout requesting %s, skipping..." % url)
        continue
    # do your stuff here
    pass
Boris Lipschitz
  • 1,514
  • 8
  • 12