如何在请求期货中使用“继续”?

Posted

技术标签:

【中文标题】如何在请求期货中使用“继续”?【英文标题】:How to use 'continue' with requests-futures? 【发布时间】:2020-07-09 03:25:29 【问题描述】:

我有一个要从网络获取结果的代码。如果互联网连接中断,我希望我的代码等待。但是当我尝试在 for 循环中执行 continue 语句时,它给了我一些棘手的结果。而且我不知道怎么解决。

代码:

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)

当我尝试离线运行它并上网时, 它一次又一次地打印“等待连接”。

输出:

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... 

我该如何解决这个问题?

【问题讨论】:

get 来自哪一行 response = get("https://google.com") 请注意 HTTP 是一种无连接协议 @Pynchia 抱歉,我忘记使用“from requests import get”而不是“import requests”。 【参考方案1】:

这背后的原因很简单,如果连接丢失,那么即使在开始时你也无法成功 使用此代码:

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)

【讨论】:

以上是关于如何在请求期货中使用“继续”?的主要内容,如果未能解决你的问题,请参考以下文章

Scala 期货 - 如何在完成时结束?

如何将自删除期货存储在列表中

如何在 Scala 中展平期货列表

如何使用杠杆下达和关闭币安期货订单

怎么用期货做风险对冲(如何利用期货对冲风险)

如何回答请求但继续在 WebApi 中处理代码