1

I have following python api function which takes some time to give output. its api endpoint is abc/abcd. when i hit that api endpoint simultaneously from multiple ip's it does not give next output till previous request is not complete. I am using aiohttp. I api should answer many requests at the same time

async def Logistic(request):
    container_id = request.query['container_id']
    company = request.query['company']
    data = mysqlcon("SELECT date,moves,location,vessel,voyage,current_status FROM container_status WHERE date_current in (SELECT max(date_current) FROM container_status) and company_id={0}".format(container_id),"rpa")
    a = {"Result":[]}
    if data == ():
        pass
    else:
        for i in data:
            a["Result"].append(i)
    return web.Response(text=json.dumps(a),headers={'ACCESS-CONTROL-ALLOW-ORIGIN':'*'})

1 Answer 1

2

Please move all database access code (SELECT ... execution and iteration over returned result) into a separate function and run it in thread pool:

def sync_db_request(container_id):
    data = mysqlcon("SELECT date,moves,location,vessel,voyage,current_status FROM container_status WHERE date_current in (SELECT max(date_current) FROM container_status) and company_id={0}".format(container_id),"rpa")
    a = {"Result":[]}
    if data == ():
        pass
    else:
        for i in data:
            a["Result"].append(i)
    return a

async def Logistic(request):
    container_id = request.query['container_id']
    company = request.query['company']
    a = await request.loop.run_in_executor(None, sync_db_request, container_id)
    return web.Response(text=json.dumps(a),headers={'ACCESS-CONTROL-ALLOW-ORIGIN':'*'})
Sign up to request clarification or add additional context in comments.

2 Comments

i will try your solution and get back to you
Its working thanks, Can you give me api authentication example using public key and private key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.