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 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
15 changes: 8 additions & 7 deletions doc/examples/tornado_change_stream_example.py
Expand Up @@ -68,7 +68,10 @@ def on_change(cls, change):

# Each change notification has a binary _id. Use it to make an HTML
# element id, then remove it.
html_id = urlsafe_b64encode(change["_id"]["_data"]).decode().rstrip("=")
data = change["_id"]["_data"]
if not isinstance(data, bytes):
data = data.encode("utf-8")
html_id = urlsafe_b64encode(data).decode().rstrip("=")
change.pop("_id")
change["html"] = '<div id="change-%s"><pre>%s</pre></div>' % (
html_id,
Expand Down Expand Up @@ -104,15 +107,13 @@ def main():
app = Application()
app.listen(options.port)
loop = tornado.ioloop.IOLoop.current()

# Start watching collection for changes.
loop.add_callback(watch, collection)
try:
loop.start()
loop.run_sync(lambda: watch(collection))
except KeyboardInterrupt:
pass
finally:
if change_stream is not None:
change_stream.close()
if change_stream:
loop.run_sync(change_stream.close)


if __name__ == "__main__":
Expand Down
20 changes: 7 additions & 13 deletions motor/core.py
Expand Up @@ -1065,23 +1065,17 @@ async def watch_collection():
def main():
loop = IOLoop.current()
# Start watching collection for changes.
loop.add_callback(watch_collection)
try:
loop.start()
except KeyboardInterrupt:
pass
finally:
if change_stream is not None:
change_stream.close()
try:
loop.run_sync(watch_collection)
except KeyboardInterrupt:
if change_stream:
loop.run_sync(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()
pass
Copy link
Contributor

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?

Copy link
Member Author

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.


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