Hi, i’m currently using a modified aiohttp
example<https://github.com/fafhrd91/aiohttp/blob/master/examples/wssrv.py>to
create a websocket server.
it contains a handle_request method that basically (leaving out connection
drops) does this:
status, headers, parser, writer = websocket.do_handshake(message.method,
message.headers, self.transport)
dataqueue = self.stream.set_parser(parser)
while True:
msg = yield from dataqueue.read()
yield from self.handle_msg(msg.data, writer)
handle_msg should now run a coroutine in the “background”, until that task
is done (then we send the result back), or the client sends “cancel”, in
which case the other coroutine should stop what it’s doing:
if data == 'cancel' and self.task:
if self.task.done():
print('already done', self.task.result())
else:
print('cancelling')
self.task.set_result('{"cancelled": true}')
self.task = Noneelif not self.task:
self.task = asyncio.async(self.do_task(data, client)) # ????
result = yield from self.task
client.send(result)else:
print('multiple tasks not yet implemented')
the problem here is that messages seem to be processed completely
sequential. no matter how soon the client sends “cancel”, it waits until
the task is done, then prints “already done {“result”: true}”
how do i create a task that is controlable from that message queue?
PS: i also tried with task.cancel(), which also doesn’t work, just like
set_result