I'm going to call the unspecified number of the counter function from inside the loop function And the counter function also call the send_request function
just i want counter do not wait for the send_request answer , when HTTP response Arrived print
import requests
import asyncio
import random
async def counter(i):
print("Started counter ",i)
await asyncio.create_task(send_request(random.randint(1,10)))
async def send_request(i):
print("Sending HTTP request ",i)
await asyncio.sleep(i)
r = requests.get('http://example.com')
print(f"Got HTTP response with status {r.status_code} in time {i}")
@app.incomeing_msg(i)
async def loop(i):
asyncio.create_task(counter(i))
asyncio.run(loop())
This page does not exist yet.loopto wait for each task, don'tawaiteach task!gatherthem once you have created all of them.await asyncio.create_task()behave different then when assigning it to a variable?requestsis a synchronous library. Don't use it in an asynchronous event loop, since it will block the entire loop – and thus all its tasks – while working.