In FastAPI, I have this route:
for id in ids: #get projects from list of ids
p = await gitlab.project(id)
if p and 'error' not in p:
projects[int(id)] = p
But it takes around 2sec per request sequentially, so I wait more than a minute.
How can I do this in parallel using like 10 threads from a thread-pool the easiest way without having to manipulate the gitlap.project(id) method
gitlab.py has a global httpx.AsyncClient()
I tried sending the ids directly:
res = await gitlab.projects(ids)
but is still do all sequentially.
Below are the two functions in gitlab.py:
async def project(id:str):
"""" return meta data for project """
global s
url = config.get_config()['gitlaburl'] + f"/{id}"
r = await s.get(url, headers={'PRIVATE-TOKEN': config.get_config()['gitlabtoken']})
if r.status_code!=200:
return {"error": f"unable to fetch from gitlab: {url}:{r.status_code} -> {r.reason}"}
out = {}
out['id'] = int(id)
dat = json.loads(r.text)
for k,v in dat.items():
if k in "description,name,path_with_namespace".split(','):
out[k] = v
if k=='namespace' and 'avatar_url' in v:
out['avatar_url'] = v['avatar_url']
return out
async def projects(ids:List[Union[str,int]]):
""" array of projects from config projectids """
dat = []
for id in ids:
dat.append(await project(id))
return dat