简体   繁体   中英

how to Async request in sanic parallel Work

Im not able to free the main function from this code so that tasks are completed in parallel and i can receive another get.

in this code when i open in chrome http://0.0.0.0:8082/envioavisos?test1=AAAAAA&test2=test the get_avisos_grupo() function is excecuted in secuence and not in parallel and untill the function ends and not able to send another request to http://0.0.0.0:8082/envioavisos?test1=AAAAAA&test2=test

#!/usr/bin/env python3
import asyncio
import time
from sanic import Sanic
from sanic.response import text
from datetime import datetime
import requests

avisos_ips = ['1.1.1.1','2.2.2.2']

app = Sanic(name='server')

async def get_avisos_grupo(ip_destino,test1,test2):
    try:
        try:
            print(datetime.now().strftime("%d/%m/%Y %H:%M:%S,%f"),'STEP 2',ip_destino)

            r = requests.post('http://{}:8081/avisosgrupo?test1={}&test2={}'.format(ip_destino,test1,test2), timeout=10)
            await asyncio.sleep(5)
        except Exception as e:
            print('TIME OUT',str(e))
            pass

    except Exception as e:
        print(str(e))
        pass


@app.route("/envioavisos", methods=['GET','POST'])
async def avisos_telegram_send(request): ## enviar avisos
    try:
        query_components = request.get_args(keep_blank_values=True)
        print(datetime.now().strftime("%d/%m/%Y %H:%M:%S,%f"),'>--------STEP 1',query_components['test1'][0])

        for ip_destino in avisos_ips:
            asyncio.ensure_future(get_avisos_grupo(ip_destino,query_components['test1'][0],query_components['test2'][0]))

    except Exception as e:
        print(str(e))
        pass
    print(datetime.now().strftime("%d/%m/%Y %H:%M:%S,%f"),'STEP 4')
    return text('ok')



if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8082, workers=4)

Expected result is to post everything in parallel.

I'm getting this result

06/04/2021 16:25:18,669074 STEP 2 1.1.1.1
TIME OUT HTTPConnectionPool(host='1.1.1.1', port=8081): Max retries exceeded with url: '))
06/04/2021 16:25:28,684200 STEP 2 2.2.2.2
TIME OUT HTTPConnectionPool(host='2.2.2.2', port=8081): Max retries exceeded with url: '))

i expect to have something like this

06/04/2021 16:25:18,669074 STEP 2 1.1.1.1
06/04/2021 16:25:28,684200 STEP 2 2.2.2.2
TIME OUT HTTPConnectionPool(host='1.1.1.1', port=8081): Max retries exceeded with url: '))
TIME OUT HTTPConnectionPool(host='2.2.2.2', port=8081): Max retries exceeded with url: '))

Asyncio is not a magic bullet that parallelizes operations. Indeed Sanic doesn't either. What it does is make efficient use of the processor to allow for multiple functions to "push the ball forward" a little at a time.

Everything runs in a single thread and a single process.

You are experiencing this because you are using a blocking HTTP call. You should replace requests with an async compatible utility so that Sanic can put the request aside to handle new requests while the outgoing operation takes place.

take a look at this:

https://sanicframework.org/en/guide/basics/handlers.html#a-word-about-async

A common mistake!

Don't do this. You need to ping a website? What do you use? pip install your-fav-request-library

Instead, try using a client that is async/await capable. Your server will thank you. Avoid using blocking tools, and favor those that play well in the asynchronous ecosystem. If you need recommendations, check out Awesome Sanic

Sanic uses httpx inside of its testing package (sanic-testing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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