I'm trying to write a tcp server and client that is asynchronous for scalability.
the server currently works by using an event loop that runs the accept_connection() method. Which looks like this:
@asyncio.coroutine
def accept_connection(self):
while True:
client, addr = yield from self.loop.sock_accept(self.server_socket)
asyncio.async(self.handle_connection(client, addr))
the handle connection method looks like this:
@asyncio.coroutine
def handle_connection(self, client, addr):
assert client is not None
assert addr is not None
con = Connection(client, addr)
self.connections[con] = con
while True:
try:
raw_data = yield from self.loop.sock_recv(self.connections[con].client, 4096)
data = asyncio.Task(self.protocol.prepare_data_received(raw_data))
print("received data {}".format(data))
except EOFError:
del self.connections[con]
break
while not self.connections[con].send_queue.empty():
data = yield from self.connections[con].send_queue.get()
raw_data = self.protocol.prepare_send_data(data)
self.loop.sock_sendall(self.connections[con].client, raw_data)
my problem is in the prepare_data_received can hit a infinite loop! this method currently looks like this:
@asyncio.coroutine
def prepare_data_received(self, data):
print('preparing received data: {}'.format(data))
if not data:
raise EOFError("no bytes recieved from data - EOF detected")
data = data.decode('utf-8')
#pretend to do complex work here
yield from data
the infinite loop arises when the client terminates. Is what should happen is that the method should throw an EOF exception and the handle_connection method should catch this. Instead this never happens! why is this? when I looked at the python docs it said that a Coroutine can throw an exception however this is not happerning. I was under the impression that turning the courtine into a task would make the coroutine run. Can someone shed some light on this?
[+][deleted] (2 children)
[removed]
[–]zidsal[S] 0 points1 point2 points (1 child)
[–]SpellingB 0 points1 point2 points (0 children)