Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getting started for -both- case #144

Merged
merged 2 commits into from Nov 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 19 additions & 21 deletions docs/intro.rst
Expand Up @@ -90,28 +90,26 @@ messages on the same connection.

::

async def handler(websocket, path):
async def consumer_handler(websocket):
while True:
message = await websocket.recv()
await consumer(message)

async def producer_handler(websocket):
while True:
listener_task = asyncio.ensure_future(websocket.recv())
producer_task = asyncio.ensure_future(producer())
done, pending = await asyncio.wait(
[listener_task, producer_task],
return_when=asyncio.FIRST_COMPLETED)

if listener_task in done:
message = listener_task.result()
await consumer(message)
else:
listener_task.cancel()

if producer_task in done:
message = producer_task.result()
await websocket.send(message)
else:
producer_task.cancel()

(This code looks convoluted. If you know a more straightforward solution,
please let me know about it!)
message = await producer()
await websocket.send(message)

async def handler(websocket, path):
consumer_task = asyncio.ensure_future(consumer_handler(websocket))
producer_task = asyncio.ensure_future(producer_handler(websocket))
done, pending = await asyncio.wait(
[consumer_task, producer_task],
return_when=asyncio.FIRST_COMPLETED,
)

for task in pending:
task.cancel()

Registration
............
Expand Down