Aufruf von Coroutinen in asyncio.Protocol.data_received
Ich habe ein Problem beim Ausführen von asynchronen Inhalten in derasyncio.Protocol.data_received
Rückruf der neuen Pythonasyncio Modul.
Betrachten Sie den folgenden Server:
class MathServer(asyncio.Protocol):
@asyncio.coroutine
def slow_sqrt(self, x):
yield from asyncio.sleep(1)
return math.sqrt(x)
def fast_sqrt(self, x):
return math.sqrt(x)
def connection_made(self, transport):
self.transport = transport
#@asyncio.coroutine
def data_received(self, data):
print('data received: {}'.format(data.decode()))
x = json.loads(data.decode())
#res = self.fast_sqrt(x)
res = yield from self.slow_sqrt(x)
self.transport.write(json.dumps(res).encode('utf8'))
self.transport.close()
wird mit dem folgenden Client verwendet:
class MathClient(asyncio.Protocol):
def connection_made(self, transport):
transport.write(json.dumps(2.).encode('utf8'))
def data_received(self, data):
print('data received: {}'.format(data.decode()))
def connection_lost(self, exc):
asyncio.get_event_loop().stop()
Mitself.fast_sqrt
wird angerufen, funktioniert alles wie erwartet.
Mitself.slow_sqrt
, es funktioniert nicht.
Es funktioniert auch nicht mitself.fast_sqrt
und das@asyncio.coroutine
Dekorateur aufdata_received
.
Mir fehlt hier etwas Grundlegendes.
Der vollständige Code ist hier:
ServerKlientGetestet mit:
Python 3.4.0b1 (Windows)Python 3.3.3 + asyncio-0.2.1 (FreeBSD)Das Problem ist bei beiden gleich: beislow_sqrt
, der Client / Server bleibt einfach hängen und tut nichts.