Issue
Let's consider this example code:
myfd = ... # my fd, can be a socket for example
def reader():
data = myfd.recv(100)
print("Received:", data.decode())
asyncio.get_event_loop().loop.add_reader(myfd, reader)
asyncio.get_event_loop().run_forever()
My issue is the asyncio.get_event_loop().run_forever()
part. I would like to await
on the closing event of myfd instead.
Solution
This is a possible solution:
myfd = ... # my fd, can be a socket for example
conn_closed = asyncio.Event() # <-- This is the solution
def reader():
try:
data = myfd.recv(100)
print("Received:", data.decode())
except EOFError: # connection closed
conn_closed.set()
except Exception as e:
print(f"Unexpected Exception: {e}")
conn_closed.set()
asyncio.get_event_loop().loop.add_reader(myfd, reader)
await conn_closed.wait()
Answered By - luca
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.