How to use 'continue' with requests-futures?

348 Views Asked by At

I have a code where I want to fetch results from the web. And I want my code to wait if the internet connection goes down. But when I tried to perform a continue statement within the for loop it's giving me some tricky results. And I don't know how to solve it.

Code:

import requests
import time
from requests.exceptions import ConnectionError
from requests_futures.sessions import FuturesSession

def wait_for_con():
    con = False
    while not con:
        try:
            response = requests.get("https://google.com")
            if response.status_code == 200:
                con = True
        except ConnectionError:
            time.sleep(1)



def run (targets):
    with FuturesSession() as session:
        futures = [session.get(target) for target in targets]
        for future in futures:
            try:
                result = future.result()
                status = result.status_code
                print (str(status)+"    "+result.url)
                
            except ConnectionError:
                print ("waitng for connection")
                wait_for_con()
                continue
run(my_url_list)

While i tried to run it offline and go online, It's printing " waiting for connection" again and again.

Output:

waiting for connection
waiting for connection
waiting for connection
waiting for connection
waiting for connection
waiting for connection
waiting for connection
waiting for connection
waiting for connection

Printing Continues... 

How can I fix this?

1

There are 1 best solutions below

0
On

The reason behind this is simple that if connection is missing then even in the beginning itself you won't be able to get success
Use This code:

import requests
import time
from requests.exceptions import ConnectionError
from requests_futures.sessions import FuturesSession


def wait_for_con():
    con = False
    while not con:
        try:
            response = requests.get("https://google.com")
            if response.status_code == 200:
                con = True
                print("connect")
        except ConnectionError:
            print("trying")
            time.sleep(1)


def run(targets):
    with FuturesSession() as session:
        futures = [session.get(target) for target in targets]
        n = len(futures)
        while (n):

            try:
                n -= 1
                result = futures[n].result()
                status = result.status_code
                print(str(status) + "    " + result.url)

            except ConnectionError:
                print("waitng for connection")
                wait_for_con()
                time.sleep(1)
                print("connection stable")
                futures = [session.get(target) for target in targets]

                # result = future.result()
                # status = result.status_code
                # print(str(status)+"    "+result.url)


my_url_list = ['https://pypi.org/project/requests-futures/',
               'https://pypi.org/project/requests-futures/',
               'https://pypi.org/project/requests-futures/',
               ]
run(my_url_list)