Skip to content

DOCSP-48170: Async examples for Serialization/Third Party Tools #231

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

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions source/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,40 @@ Serializing Custom Classes

To serialize a custom class, you must convert the class to a dictionary. The following
example serializes a custom class by using the ``vars()`` method, and then inserts the
serialized object into a collection:
serialized object into a collection. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous`
tab to see the corresponding code:

.. code-block:: python
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. code-block:: python

class Restaurant:
def __init__(self, name, cuisine):
self.name = name
self.cuisine = cuisine

restaurant = Restaurant("Example Cafe", "Coffee")
restaurant_dict = vars(restaurant)

collection.insert_one(restaurant_dict)

.. tab:: Asynchronous
:tabid: async

class Restaurant:
def __init__(self, name, cuisine):
self.name = name
self.cuisine = cuisine
.. code-block:: python

restaurant = Restaurant("Example Cafe", "Coffee")
restaurant_dict = vars(restaurant)
class Restaurant:
def __init__(self, name, cuisine):
self.name = name
self.cuisine = cuisine

restaurant = Restaurant("Example Cafe", "Coffee")
restaurant_dict = vars(restaurant)

collection.insert_one(restaurant_dict)
await collection.insert_one(restaurant_dict)

The preceding example serializes the ``Restaurant`` object into the following dictionary:

Expand All @@ -84,15 +105,32 @@ Deserializing Custom Classes

To deserialize a custom class, you must convert the dictionary back into an instance of
the class. The following example retrieves a document from a collection, and then converts
it back into a ``Restaurant`` object from the preceding example:
it back into a ``Restaurant`` object from the preceding example. Select the
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:

.. tabs::

.. tab:: Synchronous
:tabid: sync

.. code-block:: python

def deserialize_restaurant(doc):
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])

restaurant_doc = collection.find_one({"name": "Example Cafe"})
restaurant = deserialize_restaurant(restaurant_doc)

.. tab:: Asynchronous
:tabid: async

.. code-block:: python
.. code-block:: python

def deserialize_restaurant(doc):
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
def deserialize_restaurant(doc):
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])

restaurant_doc = collection.find_one({"name": "Example Cafe"})
restaurant = deserialize_restaurant(restaurant_doc)
restaurant_doc = await collection.find_one({"name": "Example Cafe"})
restaurant = deserialize_restaurant(restaurant_doc)

To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve`
guide.
Expand Down
31 changes: 24 additions & 7 deletions source/tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,32 @@ on `greenlets <https://pypi.org/project/greenlet/>`__ instead of threads.

To use gevent with {+driver-short+}, call gevent's
``monkey.patch_all()`` method *before* loading any other modules, as shown in the following
example:
example. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
corresponding code:

.. code-block:: python
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. code-block:: python

# You must call patch_all() *before* importing any other modules
from gevent import monkey
_ = monkey.patch_all()
from pymongo import MongoClient
client = MongoClient()

.. tab:: Asynchronous
:tabid: async

.. code-block:: python

# You must call patch_all() *before* importing any other modules
from gevent import monkey
_ = monkey.patch_all()
from pymongo import MongoClient
client = MongoClient()
# You must call patch_all() *before* importing any other modules

Choose a reason for hiding this comment

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

The async API doesn't support using gevent. That should be explicitly called out in the docs, where would the best place be to do so?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably either in the breaking changes page or on the forthcoming Sync vs. Async page. In the meantime, can undo this change here.

Choose a reason for hiding this comment

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

Does it make sense to add a little callout here saying that AsyncMongoClient doesn't support gevent?

from gevent import monkey
_ = monkey.patch_all()
from pymongo import AsyncMongoClient
client = AsyncMongoClient()

.. important:: Close MongoClient to Avoid Blocking

Expand Down
Loading