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

MOTOR-938 Docs for watch() incorrectly call ChangeStream.close() #163

Merged
merged 6 commits into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 1 addition & 3 deletions doc/examples/tornado_change_stream_example.py
Expand Up @@ -109,10 +109,8 @@ def main():
try:
loop.start()
except KeyboardInterrupt:
pass
finally:
if change_stream is not None:
change_stream.close()
loop.add_callback(change_stream.close)
Copy link
Member

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



if __name__ == "__main__":
Expand Down
11 changes: 4 additions & 7 deletions motor/core.py
Expand Up @@ -1069,19 +1069,16 @@ def main():
try:
loop.start()
except KeyboardInterrupt:
pass
finally:
if change_stream is not None:
change_stream.close()
loop.add_callback(change_stream.close)

# asyncio
try:
asyncio.run(watch_collection)
asyncio.run(watch_collection())
except KeyboardInterrupt:
pass
finally:
if change_stream is not None:
change_stream.close()
asyncio.run(change_stream.close())
Copy link
Member

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().

Copy link
Member Author

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.



The :class:`~MotorChangeStream` async iterable blocks
until the next change document is returned or an error is raised. If
Expand Down