Python async function not working when called outside run loop
Question:
I am writing an abstraction layer over a request-response library example. In the original example the request packet is sent to server inside an infinite loop which I have generalized here.
from their_stack import create_endpoint
async def run(remote_ip, remote_port):
server_connection = await create_endpoint(remote_ip, remote_port)
while True:
server_connection.stack_send_request() #a callback in server_connection will be called when response received
await asyncio.sleep(3)
server_connnection.close()
def main():
asyncio.run(
run("127.0.0.1", 9999)
)
I want to be able to send requests ad-hoc rather than in a loop . So I separated the send functionality as a separate function.
from their_stack import create_endpoint
async def my_send_request(remote_ip, remote_port):
server_connection = await create_endpoint(remote_ip, remote_port)
server_connection.stack_send_request()
server_connnection.close()
def main():
asyncio.run(my_send_request("127.0.0.1", 9999))
But when I run it I’m not getting the responses from the server. From the server logs I know the requests are being received by the service.
What am I missing here?
Answers:
The API works if I borrow the sleep from the original loop. A 0.1 second sleep works fine. So the final send function looks like this.
async def my_send_request(remote_ip, remote_port):
server_connection = await create_endpoint(remote_ip, remote_port)
server_connection.stack_send_request()
await asyncio.sleep(0.1)
server_connection.close()
I am writing an abstraction layer over a request-response library example. In the original example the request packet is sent to server inside an infinite loop which I have generalized here.
from their_stack import create_endpoint
async def run(remote_ip, remote_port):
server_connection = await create_endpoint(remote_ip, remote_port)
while True:
server_connection.stack_send_request() #a callback in server_connection will be called when response received
await asyncio.sleep(3)
server_connnection.close()
def main():
asyncio.run(
run("127.0.0.1", 9999)
)
I want to be able to send requests ad-hoc rather than in a loop . So I separated the send functionality as a separate function.
from their_stack import create_endpoint
async def my_send_request(remote_ip, remote_port):
server_connection = await create_endpoint(remote_ip, remote_port)
server_connection.stack_send_request()
server_connnection.close()
def main():
asyncio.run(my_send_request("127.0.0.1", 9999))
But when I run it I’m not getting the responses from the server. From the server logs I know the requests are being received by the service.
What am I missing here?
The API works if I borrow the sleep from the original loop. A 0.1 second sleep works fine. So the final send function looks like this.
async def my_send_request(remote_ip, remote_port):
server_connection = await create_endpoint(remote_ip, remote_port)
server_connection.stack_send_request()
await asyncio.sleep(0.1)
server_connection.close()