在API上进行循环 [英] For loop on API

查看:142
本文介绍了在API上进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为" y "的列表中有大约28000个数字的列表,并且我正在API上运行for循环以发送消息,但这需要花费很多时间(准确地说是1.2797秒)每次通话)

I have a list of around 28K numbers in a list named "y" and I am running a for loop on API to send Messages but this takes a lot of time (to be exact 1.2797 seconds per call)

代码:

import timeit

start = timeit.default_timer()

for i in y:
    data = {'From': 'XXXX', 'To': str(i),
            'Body': "ABC ABC" }
    requests.post('https://xxxx:xx@api.xxx.com/v1/Accounts/xxx/Sms/send',data=data)

stop = timeit.default_timer()
print('Time: ', stop - start)   

我该如何减少时间?

推荐答案

Asyncio或多线程处理是优化代码的两种可能的解决方案,并且两者基本上都在后台执行相同的操作:

Asyncio or Multithreading are the two possible solutions to optimize your code, and both basically do the same under the hood:

import timeit
import threading
import time

y = list(range(50))


def post_data(server, data, sleep_time=1.5):
    time.sleep(sleep_time)
    # request.post(server, data=data)


start = timeit.default_timer()

server = 'https://xxxx:xx@api.xxx.com/v1/Accounts/xxx/Sms/send'

threads = []
for i in y:
    # if you don't need to wait for your threads don't hold them in memory after they are done and instead do
    # threading.Thread(target, args).start()
    # instead. Especially important if you want to send a large number of messages
    threads.append(threading.Thread(target=post_data,
                            args=(server, {'From': 'XXXX', 'To': str(i), 'Body': "ABC ABC"}))
    threads[-1].start()

for thread in threads:
    # optional if you want to wait for completion of the concurrent posts
    thread.join()

stop = timeit.default_timer()
print('Time: ', stop - start)

异步

引用此答案.

import timeit
import asyncio
from concurrent.futures import ThreadPoolExecutor

y =  list(range(50)
_executor = ThreadPoolExecutor(len(y))

loop = asyncio.get_event_loop()

def post_data(server, data, sleep_time=1.5):
    time.sleep(sleep_time)
    # request.post(server, data=data)

async def post_data_async(server, data):
    return await loop.run_in_executor(_executor, lambda: post_data(server, data))


async def run(y, server):
    return await asyncio.gather(*[post_data_async(server, {'From': 'XXXX', 'To': str(i), 'Body': "ABC ABC"})
                                  for i in y])


start = timeit.default_timer()

server = 'https://xxxx:xx@api.xxx.com/v1/Accounts/xxx/Sms/send'

loop.run_until_complete(run(y, server))

stop = timeit.default_timer()
print('Time: ', stop - start)

当使用不支持异步但可以从并发中获利的API时(如您的用例),我倾向于使用线程,因为它更易于阅读恕我直言.如果您的API/库确实支持asyncio,那就去吧!太好了!

When using an API that does not support asyncio but would profit from concurrency, like your use-case, I'd tend towards using threading as it's easier to read IMHO. If your API/Library does support asyncio, go for it! It's great!

在我的包含50个元素的列表的机器上,asyncio解决方案在运行1.515秒时运行,而在执行50个time.sleep(1.5)实例时,线程解决方案大约需要1.509秒.

On my machine with a list of 50 elements the asyncio solutions clocks in at 1.515 seconds of runtime while the threaded solution needs about 1.509 seconds, when executing 50 instances of time.sleep(1.5).

这篇关于在API上进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆