python异步发布请求 [英] python async post requests

查看:51
本文介绍了python异步发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以使这个脚本更快 - 例如立即创建 1000 个帐户或至少在几秒钟内创建.我已经尝试过自己做一些异步的东西,但这是我所能得到的,我只是一个异步编程的初学者,所以感谢任何帮助.

I was wondering if there was any way to make this script a lot faster - like instantly create 1000 accounts for example or at least in a matter of a few seconds. I’ve tried doing some async stuff myself but this is as far as I could get, I am just a beginner with asynchronous programming so any help is appreciated.

import asyncio
import aiohttp


async def make_numbers(numbers, _numbers):
    for i in range(numbers, _numbers):
        yield i

async def make_account():
   url = "https://example.com/sign_up.php"
   async with aiohttp.ClientSession() as session:
          async for x in make_numbers(35691, 5000000):
              async with  session.post(url, data ={
                    "terms": 1,
                    "captcha": 1,
                    "email": "user%s@hotmail.com" % str(x),
                    "full_name": "user%s" % str(x),
                    "password": "123456",
                    "username": "auser%s" % str(x)
              }) as response:
                    data = await response.text()
                    print("-> Creating account number %d" % x)
                    print (data)

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(make_account())
finally:
    loop.close()

推荐答案

问题中的代码执行一个系列的所有 POST 请求,使代码不会比在单个使用 requests 时更快线.但与 requests 不同的是,asyncio 可以在同一个线程中并行化它们:

The code in the question executes all POST requests in a series, making the code no faster than if you used requests in a single thread. But unlike requests, asyncio makes it possible to parallelize them in the same thread:

async def make_account():
    url = "https://example.com/sign_up.php"
    async with aiohttp.ClientSession() as session:
        post_tasks = []
        # prepare the coroutines that post
        async for x in make_numbers(35691, 5000000):
            post_tasks.append(do_post(session, url, x))
        # now execute them all at once
        await asyncio.gather(*post_tasks)

async def do_post(session, url, x):
    async with session.post(url, data ={
                "terms": 1,
                "captcha": 1,
                "email": "user%s@hotmail.com" % str(x),
                "full_name": "user%s" % str(x),
                "password": "123456",
                "username": "auser%s" % str(x)
          }) as response:
          data = await response.text()
          print("-> Created account number %d" % x)
          print (data)

上面的代码将尝试一次发送所有 POST 请求.尽管如此,它仍会受到 aiohttp.ClientSession 的 TCP 连接器的限制,该连接器默认允许最多 100 个同时连接.要增加或删除此限制,您必须设置 自定义连接器 在会议上.

The above code will attempt to send all the POST requests at once. Despite the intention, it will be throttled by aiohttp.ClientSession's TCP connector which allows a maximum of 100 simultaneous connections by default. To increase or remove this limitation, you must set a custom connector on the session.

这篇关于python异步发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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