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

Fix ws close faq #2208

Merged
merged 5 commits into from
Aug 24, 2017
Merged

Fix ws close faq #2208

merged 5 commits into from
Aug 24, 2017

Conversation

mpaolini
Copy link
Contributor

Update FAQ entry to leverage #754

Due to improvements done in aio-libs#754, websocket `close()`
can now be called from a separate task.
@mpaolini mpaolini changed the base branch from 2.2 to master August 16, 2017 08:29
@codecov-io
Copy link

codecov-io commented Aug 16, 2017

Codecov Report

Merging #2208 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2208   +/-   ##
=======================================
  Coverage   97.15%   97.15%           
=======================================
  Files          39       39           
  Lines        7895     7895           
  Branches     1367     1367           
=======================================
  Hits         7670     7670           
  Misses        100      100           
  Partials      125      125

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4ffbd4c...755da51. Read the comment docs.

docs/faq.rst Outdated

async def echo_handler(request):

ws = web.WebSocketResponse()
ws = aiohttp.web.WebSocketResponse()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep web.WebSocketResponse name.
Canonical way for using aiohttp web is from aiohttp import web.

docs/faq.rst Outdated
# return response
...
# Watch out, this will keep us from returing the response until all are closed
ws_closers and await asyncio.wait(ws_closers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aynsio.wait doesn't raise exceptions, please use asyncio.gather().

docs/faq.rst Outdated
# Watch out, this will keep us from returing the response until all are closed
ws_closers and await asyncio.wait(ws_closers)

return aiohttp.web.Response(text='OK')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name

docs/faq.rst Outdated

def main():
loop = asyncio.get_event_loop()
app = aiohttp.web.Application(loop=loop)
app.router.add_route('GET', '/echo', echo_handler)
app.router.add_route('POST', '/logout', logout_handler)
app['websockets'] = defaultdict(set)
app['websockets'] = defaultdict(deque)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where deque comes from and why set is not enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocketResponse is not hashable, that's why I used collections.deque

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix notes.

docs/faq.rst Outdated

async def echo_handler(request):

ws = web.WebSocketResponse()
user_id = authenticate_user(request)
await ws.prepare(request)
request.app['websockets'][user_id].add(asyncio.Task.current_task())

request.app['websockets'][user_id][id(ws)] = ws
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id(ws) is not reliable: after garbage collection there is a chance to create new web.WebSocketResponse() at the same memory address.

@asvetlov asvetlov merged commit d951059 into aio-libs:master Aug 24, 2017
@asvetlov
Copy link
Member

Thanks!

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants