-
Notifications
You must be signed in to change notification settings - Fork 210
MOTOR-938 Docs for watch() incorrectly call ChangeStream.close() #163
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
Conversation
motor/core.py
Outdated
if change_stream is not None: | ||
change_stream.close() | ||
asyncio.run(change_stream.close()) |
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.
Can you confirm that the close() is not needed? I believe the async with collection.watch() as change_stream
line will handle closing the cursor when KeyboardInterrupt is raised to asycnio.run().
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.
I did some local testing, ensuring that the change_stream was actually closed. asyncio.run
properly runs __aexit__()
, closing the stream. For the tornado loop, I had to manually run the close operation.
if change_stream is not None: | ||
change_stream.close() | ||
loop.add_callback(change_stream.close) |
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.
Does this actually work? I would expect close to never run since the loop is not running at this point. What if we use loop.run_sync similar to asyncio.run?:
loop = tornado.ioloop.IOLoop.current()
# Start watching collection for changes.
try:
loop.run_sync(lambda: watch(collection))
except KeyboardInterrupt:
pass
finally: | ||
if change_stream is not None: | ||
change_stream.close() | ||
pass |
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.
Does this pass need to be here?
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.
Yes, that or a comment, or it would be a syntax error.
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.
LGTM!
No description provided.