-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix ws close faq #2208
Conversation
Due to improvements done in aio-libs#754, websocket `close()` can now be called from a separate task.
Codecov Report
@@ 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.
|
docs/faq.rst
Outdated
|
||
async def echo_handler(request): | ||
|
||
ws = web.WebSocketResponse() | ||
ws = aiohttp.web.WebSocketResponse() |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
Thanks! |
Update FAQ entry to leverage #754