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

PYTHON-5081 Convert test.test_gridfs to async #2099

Merged
merged 15 commits into from
Feb 11, 2025

Conversation

sleepyStick
Copy link
Contributor

No description provided.

@sleepyStick sleepyStick marked this pull request as ready for review January 30, 2025 18:56
@sleepyStick sleepyStick requested a review from NoahStapp January 30, 2025 18:56
@@ -231,7 +231,7 @@ async def get_version(
try:
doc = await anext(cursor)
return AsyncGridOut(self._collection, file_document=doc, session=session)
except StopIteration:
except (StopIteration, StopAsyncIteration):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except (StopIteration, StopAsyncIteration):
except StopAsyncIteration:

The asynchronous API should only throw StopAsyncIteration, and the synchronous API should only throw StopIteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, that makes sense HAHA silly me

assert data == b"hello"
else:

class JustWrite(asyncio.Task):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to subclass Task, but have the async JustWrite class have a self.task instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, that's what we said today in OH just changed it :)

self.n = n
self.daemon = True

class JustRead(asyncio.Task):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@sleepyStick sleepyStick requested a review from NoahStapp January 30, 2025 21:23

class JustWrite:
def __init__(self, fs, n):
async def run():
Copy link
Contributor

@NoahStapp NoahStapp Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a separate method, not nested inside __init__.


class JustRead:
def __init__(self, fs, n, results):
async def run():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

test/utils.py Outdated
async def asyncjoinall(tasks):
"""Join tasks with a 5-minute timeout, assert joins succeeded"""
for t in tasks:
await asyncio.wait_for(t.task, 300)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await asyncio.wait_for(t.task, 300)
await asyncio.wait([t.task], timeout=300)

wait_for will throw a TimeoutError if it times out, wait will just return the task that didn't complete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh i didn't know that!

@sleepyStick sleepyStick requested a review from NoahStapp January 30, 2025 21:59
NoahStapp
NoahStapp previously approved these changes Jan 30, 2025
Copy link
Contributor

@NoahStapp NoahStapp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending Shane's review.

test/utils.py Outdated
async def asyncjoinall(tasks):
"""Join tasks with a 5-minute timeout, assert joins succeeded"""
for t in tasks:
await asyncio.wait([t.task], timeout=300)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use gather() to join all tasks at once.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better yet, use asyncio.wait.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea! done!


if _IS_SYNC:

class JustWrite(threading.Thread):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the shared thread/task api that Noah introduced here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! Just changed that!

if _IS_SYNC:
joinall(tasks)
else:
await asyncio.wait([t.task for t in tasks if t.task is not None])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this logic into an async_joinall method to avoid this branch. Also, there should be a 5-minute timeout on this wait to match joinall.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, done!

if _IS_SYNC:
joinall(tasks)
else:
await asyncio.wait([t.task for t in tasks if t.task is not None])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@sleepyStick sleepyStick requested a review from NoahStapp February 6, 2025 18:24
NoahStapp
NoahStapp previously approved these changes Feb 6, 2025
"test_load_balancer.py",
"test_json_util_integration.py",
"test_gridfs_spec.py",
"test_json_util_integration.py",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_json_util_integration is duplicated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch, sorry for missing that!

@sleepyStick sleepyStick merged commit 1f7f8a9 into mongodb:master Feb 11, 2025
46 of 48 checks passed
@sleepyStick sleepyStick deleted the PYTHON-5081 branch February 11, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants