Skip to content

Commit

Permalink
multiprocessing mode for init (#781)
Browse files Browse the repository at this point in the history
* multiprocessing mode for init

* version: 1.23.5
  • Loading branch information
roman-right committed Nov 13, 2023
1 parent bc854e7 commit f9f96a9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from beanie.odm.utils.init import init_beanie
from beanie.odm.views import View

__version__ = "1.23.4"
__version__ = "1.23.5"
__all__ = [
# ODM
"Document",
Expand Down
13 changes: 13 additions & 0 deletions beanie/odm/utils/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
] = None,
allow_index_dropping: bool = False,
recreate_views: bool = False,
multiprocessing_mode: bool = False,
):
"""
Beanie initializer
Expand All @@ -74,8 +75,12 @@ def __init__(
or strings with dot separated paths
:param allow_index_dropping: bool - if index dropping is allowed.
Default False
:param recreate_views: bool - if views should be recreated. Default False
:param multiprocessing_mode: bool - if multiprocessing mode is on
it will patch the motor client to use process's event loop.
:return: None
"""

self.inited_classes: List[Type] = []
self.allow_index_dropping = allow_index_dropping
self.recreate_views = recreate_views
Expand All @@ -98,6 +103,9 @@ def __init__(

self.database: AsyncIOMotorDatabase = database

if multiprocessing_mode:
self.database.client.get_io_loop = self.database.get_io_loop

sort_order = {
ModelType.UnionDoc: 0,
ModelType.Document: 1,
Expand Down Expand Up @@ -725,6 +733,7 @@ async def init_beanie(
] = None,
allow_index_dropping: bool = False,
recreate_views: bool = False,
multiprocessing_mode: bool = False,
):
"""
Beanie initialization
Expand All @@ -735,6 +744,9 @@ async def init_beanie(
or strings with dot separated paths
:param allow_index_dropping: bool - if index dropping is allowed.
Default False
:param recreate_views: bool - if views should be recreated. Default False
:param multiprocessing_mode: bool - if multiprocessing mode is on
it will patch the motor client to use process's event loop. Default False
:return: None
"""

Expand All @@ -744,4 +756,5 @@ async def init_beanie(
document_models=document_models,
allow_index_dropping=allow_index_dropping,
recreate_views=recreate_views,
multiprocessing_mode=multiprocessing_mode,
)
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Beanie project

## [1.23.5] - 2023-11-12
### Multiprocessing Mode For Init
- Author - [Roman Right](https://github.com/roman-right)
- PR <https://github.com/roman-right/beanie/pull/781>

[1.23.5]: https://pypi.org/project/beanie/1.23.5

## [1.23.4] - 2023-11-12
### Args For `get_model_dump`
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "beanie"
version = "1.23.4"
version = "1.23.5"
description = "Asynchronous Python ODM for MongoDB"
readme = "README.md"
requires-python = ">=3.7,<4.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.23.4"
assert __version__ == "1.23.5"

0 comments on commit f9f96a9

Please sign in to comment.