简体   繁体   English

如何在将列表中的每个值传递到 API 并在每个列表列表后暂停时迭代列表列表?

[英]How to iterate over list of lists while passing each value in the lists into API and pausing after each list of list?

I have a list that looks like this:我有一个看起来像这样的列表:

lst = [1,2,3, etc]

I can successfully iterate over this list and pass the IDs into an API.我可以成功地遍历此列表并将 ID 传递给 API。 However the API breaks after certain point in regards to number of Ids I am passing.但是,在我传递的 Id 数量方面,API 在某些点之后会中断。

I went ahead and broke the list into chunks of 50 using this code:我继续使用以下代码将列表分成 50 个块:

def chunks(l, n):
    # For item i in a range that is a length of l,
    for i in range(0, len(l), n):
        # Create an index range for l of n items:
        yield l[i:i+n]

a = list(chunks(lst, 50))

The a variable is list of list that looks like this: a变量是如下所示的列表列表:

[[1,2,3][4,5,6]]

How do I iterate over the list of list, passing individual Ids into the API code (below), while pausing the code for 10seconds after every chunk?如何遍历列表列表,将单个 Id 传递到 API 代码(如下),同时在每个块后暂停代码 10 秒?

In addition, if the API breaks, is there anyway I can force the API to continue rest of the code ?此外,如果 API 中断,我是否可以强制 API 继续其余代码? Not a requirement for now, so this is optional.现在不是必需的,所以这是可选的。

API code:接口代码:

lst1=[]
for i in lst:
    #print (row)
    url = 'url/Id={}'.format(i)
    r = requests.get(url).text
    lst1.append(xml_data1)
    #time.sleep(1)
    print(xml_data1)

Thank you in advance.先感谢您。

You can use a double for loop with use of the sleep function after execution of the inner loop.您可以在执行内循环后使用sleep功能使用双for循环。 Here's an example:下面是一个例子:

lst1 = []
for chunk in a:
    for id in chunk:
        url = 'url/Id={}'.format(id)
        xml_data = requests.get(url).text
        lst1.append(xml_data)
     sleep(10) # will pause execution for 10 seconds

To answer your second question about how to continue execution if the API throws an error, you can use try/except .要回答关于在 API 抛出错误时如何继续执行的第二个问题,您可以使用try/except Code that might throw an error goes in the try block and if an error is thrown, execution moves to the except block, which specifies how to proceed.可能抛出错误的代码进入try块,如果抛出错误,则执行移动到except块,该块指定如何继续。 If you wrap your code in a function this can be made a bit easier.如果您将代码包装在一个函数中,这会变得更容易一些。

I find it helpful to track the number of times you've tried calling for a specific id with a keyword argument that can be used for exponential backoff -- where you wait for longer amounts of time depending on how many times you've tried.我发现跟踪您尝试使用可用于指数退避的关键字参数调用特定id的次数很有帮助——根据您尝试的次数,您等待的时间更长。

Here is an example building on the above code:以下是基于上述代码构建的示例:

def get_data_on_id(id, try_num=1):
    url = 'url/Id={}'.format(id)
    try:
        return requests.get(url).text
    except:
        sleep(2**try_num) # exponential backoff
        return get_data_on_id(id, try_num=try_num + 1)

lst1 = []
for chunk in a:
    for id in chunk:
        xml_data = get_data_on_id(id)
        lst1.append(xml_data)
    sleep(10)

The code above is largely the same as the code before it, but the request logic is wrapped in the get_data_on_id function, which contains the try/except blocks.上面的代码与之前的代码大致相同,但请求逻辑被封装在get_data_on_id函数中,该函数包含try/except块。 If an error is thrown when requesting the url , the function will wait for 2^try_num seconds and then try again.如果请求url时抛出错误,该函数将等待2^try_num秒,然后重试。

You might also want to except specific errors and handle them differently (or just to be explicit so you only except errors you are prepared to handle).您可能还需要except特定的错误和不同的方式处理它们(或只是为了让您只明确except你准备来处理错误)。 Here are the docs on try/except : https://docs.python.org/3/tutorial/errors.html以下是关于try/except的文档: https : //docs.python.org/3/tutorial/errors.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM