-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4669 - Update More APIs for Motor Compatibility #1815
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1166,20 +1166,22 @@ def __getattr__(self, name: str) -> Any: | |
raise AttributeError("GridIn object has no attribute '%s'" % name) | ||
|
||
def __setattr__(self, name: str, value: Any) -> None: | ||
if _IS_SYNC: | ||
# For properties of this instance like _buffer, or descriptors set on | ||
# the class like filename, use regular __setattr__ | ||
if name in self.__dict__ or name in self.__class__.__dict__: | ||
object.__setattr__(self, name, value) | ||
else: | ||
# For properties of this instance like _buffer, or descriptors set on | ||
# the class like filename, use regular __setattr__ | ||
if name in self.__dict__ or name in self.__class__.__dict__: | ||
object.__setattr__(self, name, value) | ||
else: | ||
if _IS_SYNC: | ||
# All other attributes are part of the document in db.fs.files. | ||
# Store them to be sent to server on close() or if closed, send | ||
# them now. | ||
self._file[name] = value | ||
if self._closed: | ||
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}}) | ||
else: | ||
object.__setattr__(self, name, value) | ||
else: | ||
raise AttributeError( | ||
"GridIn does not support __setattr__. Use GridIn.set() instead" | ||
) | ||
|
||
def set(self, name: str, value: Any) -> None: | ||
# For properties of this instance like _buffer, or descriptors set on | ||
|
@@ -1472,6 +1474,15 @@ def __init__( | |
_file: Any | ||
_chunk_iter: Any | ||
|
||
def __next__(self) -> bytes: | ||
return super().__next__() | ||
|
||
def __next__(self) -> bytes: # noqa: F811, RUF100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any way to avoid the duplicate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a limitation of the synchro script: it will translate the async There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but can we workaround that? The duplicate code is strange the read. There's also a runtime perf cost to overriding a method just to call the super class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't a simple way to workaround it, no. We could change the definition to be less confusing, like this:
Which would synchronize to
We can also add a comment explaining why the duplicate def exists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, but how about: if not _IS_SYNC:
async def __anext__(self) -> bytes:
return await self.readline()
def __next__(self) -> bytes: # noqa: F811, RUF100
raise TypeError(
"AsyncGridOut does not support synchronous iteration. Use `async for` instead"
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot we had our own async There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that's what IOBase is supposed to do and AsyncGridOut iteration should match the sync version. We also need to remove IOBase from the async. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, follow-up PR: #1821. |
||
if _IS_SYNC: | ||
return super().__next__() | ||
else: | ||
raise TypeError("GridOut does not support synchronous iteration. Use `for` instead") | ||
|
||
def open(self) -> None: | ||
if not self._file: | ||
_disallow_transactions(self._session) | ||
|
@@ -1499,6 +1510,7 @@ def readchunk(self) -> bytes: | |
"""Reads a chunk at a time. If the current position is within a | ||
chunk the remainder of the chunk is returned. | ||
""" | ||
self.open() | ||
received = len(self._buffer) - self._buffer_pos | ||
chunk_data = EMPTY | ||
chunk_size = int(self.chunk_size) | ||
|
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.
This is incorrect. We can't call
super().__next__()
because that does blocking I/O.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.
Hmm, good catch. We don't have an async equivalent here unless we write one ourselves.
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.
IOBase implements next using readline:
https://docs.python.org/3/library/io.html#io.IOBase
Uh oh!
There was an error while loading. Please reload this page.
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.
There isn't an
asyncio
version ofreadline
, so we'd need to write our own. The canonical way to do so appears to be with threads (https://stackoverflow.com/questions/34699948/does-asyncio-supports-asynchronous-i-o-for-file-operations), at which point I question if the performance gained by not blocking the loop is more than the cost of thread overhead. The official CPython forums have similar concerns at the OS level: https://discuss.python.org/t/asyncio-for-files/31077/15.