繁体   English   中英

如何通过线程加快发送请求

[英]How can I speed up sending requests via threading

from uuid import uuid4
import requests
from time import sleep

headers={
 "cookies":"__cfduid=d0c98f07df2594b5f4aad802942cae1f01619569096",
        "authorization":"Basic NWJiNTM0OWUxYzlkNDQwMDA2NzUwNjgwOmM0ZDJmYmIxLTVlYjItNDM5MC05MDk3LTkxZjlmMjQ5NDI4OA=="
    }
uid = "0"
data = {
      "reward":{"ad_unit_id":"255884441431980_807351306285288","credentials_type":"publisher","custom_json":{"hashed_user_id":f"{uid}"},"demand_type":"sdk_bidding","event_id":f"{uuid4()}","network":"facebook","placement_tag":"default","reward_name":"Amino Coin","reward_valid":"true","reward_value":2,"shared_id":"dc042f0c-0c80-4dfd-9fde-87a5979d0d2f","version_id":"1569147951493","waterfall_id":"dc042f0c-0c80-4dfd-9fde-87a5979d0d2f"},
        "app":{"bundle_id":"com.narvii.amino.master","current_orientation":"portrait","release_version":"3.4.33567","user_agent":"Dalvik\/2.1.0 (Linux; U; Android 10; G8231 Build\/41.2.A.0.219; com.narvii.amino.master\/3.4.33567)"},"date_created":1620295485,"session_id":"49374c2c-1aa3-4094-b603-1cf2720dca67","device_user":{"country":"US","device":{"architecture":"aarch64","carrier":{"country_code":602,"name":"Vodafone","network_code":0},"is_phone":"true","model":"GT-S5360","model_type":"Samsung","operating_system":"android","operating_system_version":"29","screen_size":{"height":2260,"resolution":2.55,"width":1080}},"do_not_track":"false","idfa":"7495ec00-0490-4d53-8b9-b5cc31ba885b","ip_address":"","locale":"en","timezone":{"location":"Asia\/Seoul","offset":"GMT+09:00"},"volume_enabled":"true"}}
for i in range(25):
        respone = requests.post("https://ads.tapdaq.com/v4/analytics/reward",json=data, headers=headers)
        print(respone)

我假设每个 POST 都需要一个新的 UUID。 这应该让您知道如何进行:

from uuid import uuid4
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
import time

ITERATIONS = 25

headers = {
    "cookies": "__cfduid=d0c98f07df2594b5f4aad802942cae1f01619569096",
    "authorization": "Basic NWJiNTM0OWUxYzlkNDQwMDA2NzUwNjgwOmM0ZDJmYmIxLTVlYjItNDM5MC05MDk3LTkxZjlmMjQ5NDI4OA=="
}
data = {
    "reward": {"ad_unit_id": "255884441431980_807351306285288", "credentials_type": "publisher", "custom_json": {"hashed_user_id": "0"}, "demand_type": "sdk_bidding", "network": "facebook", "placement_tag": "default", "reward_name": "Amino Coin", "reward_valid": "true", "reward_value": 2, "shared_id": "dc042f0c-0c80-4dfd-9fde-87a5979d0d2f", "version_id": "1569147951493", "waterfall_id": "dc042f0c-0c80-4dfd-9fde-87a5979d0d2f"},
    "app": {"bundle_id": "com.narvii.amino.master", "current_orientation": "portrait", "release_version": "3.4.33567", "user_agent": "Dalvik\/2.1.0 (Linux; U; Android 10; G8231 Build\/41.2.A.0.219; com.narvii.amino.master\/3.4.33567)"}, "date_created": 1620295485, "session_id": "49374c2c-1aa3-4094-b603-1cf2720dca67", "device_user": {"country": "US", "device": {"architecture": "aarch64", "carrier": {"country_code": 602, "name": "Vodafone", "network_code": 0}, "is_phone": "true", "model": "GT-S5360", "model_type": "Samsung", "operating_system": "android", "operating_system_version": "29", "screen_size": {"height": 2260, "resolution": 2.55, "width": 1080}}, "do_not_track": "false", "idfa": "7495ec00-0490-4d53-8b9-b5cc31ba885b", "ip_address": "", "locale": "en", "timezone": {"location": "Asia\/Seoul", "offset": "GMT+09:00"}, "volume_enabled": "true"}}


def doPost():
    data['reward']['event_id'] = f'{uuid4()}'
    requests.post("https://ads.tapdaq.com/v4/analytics/reward",
                  json=data, headers=headers).raise_for_status()


def main():
    with ThreadPoolExecutor() as executor:
        for f in as_completed([executor.submit(doPost) for _ in range(ITERATIONS)]):
            f.result()


if __name__ == '__main__':
    s = time.perf_counter()
    main()
    print(f'Duration={time.perf_counter()-s:.4f}s')

暂无
暂无

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

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