Navigation Menu

Skip to content

Commit

Permalink
feat: lazy parsing docs (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-right committed Dec 17, 2022
1 parent f532a22 commit 17037b9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions-publish-project.yml
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: install dependencies
run: poetry install
- name: publish project
run: poetry publish --build --username ${{ secrets.PYPI_USERNAME }} --password ${{ secrets.PYPI_PASSWORD }}
run: poetry publish --build --username __token__ --password ${{ secrets.PYPI_TOKEN }}
12 changes: 12 additions & 0 deletions docs/tutorial/lazy_parse.md
@@ -0,0 +1,12 @@
## Using Lazy Parsing in Queries
Lazy parsing allows you to skip the parsing and validation process for documents and instead call it on demand for each field separately. This can be useful for optimizing performance in certain scenarios.

To use lazy parsing in your queries, you can pass the `lazy_parse=True` parameter to your find method.

Here's an example of how to use lazy parsing in a find query:

```python
await Sample.find(Sample.number == 10, lazy_parse=True).to_list()
```

By setting lazy_parse=True, the parsing and validation process will be skipped and be called on demand when the respective fields will be used. This can potentially improve the performance of your query by reducing the amount of processing required upfront. However, keep in mind that using lazy parsing may also introduce some additional overhead when accessing the fields later on.
2 changes: 2 additions & 0 deletions pydoc-markdown.yml
Expand Up @@ -69,6 +69,8 @@ renderer:
source: docs/tutorial/insert.md
- title: Finding documents
source: docs/tutorial/find.md
- title: Lazy parsing
source: docs/tutorial/lazy_parse.md
- title: Updating & Deleting
source: docs/tutorial/update.md
- title: Indexes
Expand Down
59 changes: 59 additions & 0 deletions tests/odm/test_concurrency.py
@@ -0,0 +1,59 @@
import asyncio

import motor.motor_asyncio

from beanie import Document, init_beanie


class SampleModel(Document):
s: str = "TEST"
i: int = 10


class SampleModel2(SampleModel):
...


class SampleModel3(SampleModel2):
...


class TestConcurrency:
async def test_with_init(self, settings):
for i in range(10):

async def init_insert_find():
cli = motor.motor_asyncio.AsyncIOMotorClient(
settings.mongodb_dsn
)
cli.get_io_loop = asyncio.get_running_loop
db = cli[settings.mongodb_db_name]
await init_beanie(
db,
document_models=[SampleModel3, SampleModel, SampleModel2],
)

await SampleModel2().insert()

docs = await SampleModel2.find(SampleModel.i == 10).to_list()
return docs

await asyncio.gather(*[init_insert_find() for _ in range(100)])
await SampleModel2.delete_all()

async def test_without_init(self, settings):
for i in range(10):
cli = motor.motor_asyncio.AsyncIOMotorClient(settings.mongodb_dsn)
cli.get_io_loop = asyncio.get_running_loop
db = cli[settings.mongodb_db_name]
await init_beanie(
db, document_models=[SampleModel3, SampleModel, SampleModel2]
)

async def insert_find():
await SampleModel2().insert()
docs = await SampleModel2.find(SampleModel2.i == 10).to_list()
return docs

await asyncio.gather(*[insert_find() for _ in range(100)])
await SampleModel2.delete_all()

0 comments on commit 17037b9

Please sign in to comment.