Use threads to speed-up parallel requests

This commit is contained in:
Mikhail f. Shiryaev 2021-12-06 12:21:42 +01:00
parent ccf6210eb1
commit 31a5afe855
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -4,6 +4,9 @@ import requests
import argparse import argparse
import json import json
from threading import Thread
from queue import Queue
def get_org_team_members(token: str, org: str, team_slug: str) -> tuple: def get_org_team_members(token: str, org: str, team_slug: str) -> tuple:
headers = { headers = {
@ -19,14 +22,42 @@ def get_org_team_members(token: str, org: str, team_slug: str) -> tuple:
def get_members_keys(members: tuple) -> str: def get_members_keys(members: tuple) -> str:
keys = "" class Worker(Thread):
def __init__(self, request_queue):
Thread.__init__(self)
self.queue = request_queue
self.results = []
def run(self):
while True:
m = self.queue.get()
if m == "":
break
response = requests.get(f"https://github.com/{m}.keys")
self.results.append(f"# {m}\n{response.text}")
self.queue.task_done()
q = Queue()
workers = []
for m in members: for m in members:
response = requests.get( q.put(m)
f"https://github.com/{m}.keys", # Create workers and add to the queue
) worker = Worker(q)
response.raise_for_status() worker.start()
keys += f"# {m}\n{response.text}" workers.append(worker)
return keys
# Workers keep working till they receive an empty string
for _ in workers:
q.put("")
# Join workers to wait till they finished
for worker in workers:
worker.join()
responses = []
for worker in workers:
responses.extend(worker.results)
return "".join(responses)
def get_token_from_aws() -> str: def get_token_from_aws() -> str:
@ -51,7 +82,14 @@ def main(token: str, org: str, team_slug: str) -> str:
def handler(event, context): def handler(event, context):
token = get_token_from_aws() token = get_token_from_aws()
return main(token, "ClickHouse", "core") result = {
"statusCode": 200,
"headers": {
"Content-Type": "text/html",
},
"body": main(token, "ClickHouse", "core"),
}
return result
if __name__ == "__main__": if __name__ == "__main__":