Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions config/redirects
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ raw: ${prefix}/master -> ${base}/upcoming/
raw: ${prefix}/get-started/download-and-install/ -> ${base}/current/get-started/download-and-install/

[*-master]: ${prefix}/${version}/security/enterprise-authentication/ -> ${base}/${version}/security/authentication/
[*-master]: ${prefix}/${version}/faq/ -> ${base}/${version}/
5 changes: 1 addition & 4 deletions source/compatibility.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,4 @@ The following compatibility table specifies the recommended version of
{+driver-short+} for use with a specific version of Python.
The first column lists the driver version.

.. include:: /includes/language-compatibility-table-pymongo.rst

For more information on how to read the compatibility tables, see our guide on
:ref:`MongoDB Compatibility Tables. <about-driver-compatibility>`
.. include:: /includes/language-compatibility-table-pymongo.rst
59 changes: 53 additions & 6 deletions source/connect/mongoclient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,60 @@ constructor accepts. All parameters are optional.

**Data type:** `TypeRegistry <{+api-root+}bson/codec_options.html#bson.codec_options.TypeRegistry>`__

.. tip:: Reusing Your Client
Concurrent Execution
--------------------

Because each ``MongoClient`` object represents a pool of connections to the
database, most applications require only a single instance of
``MongoClient``, even across multiple requests. However, if you fork
a process, the child process *does* need its own ``MongoClient`` object.
To learn more, see the :ref:`FAQ <pymongo-faq>` page.
The following sections describe {+driver-short+}'s support for concurrent execution
mechanisms.

Multithreading
~~~~~~~~~~~~~~

{+driver-short+} is thread-safe and provides built-in connection pooling
for threaded applications.
Because each ``MongoClient`` object represents a pool of connections to the
database, most applications require only a single instance of
``MongoClient``, even across multiple requests.

.. _pymongo-forks:

Multiple Forks
~~~~~~~~~~~~~~~

{+driver-short+} supports calling the ``fork()`` method to create a new process.
However, if you fork a process, you must create a new ``MongoClient`` instance in the
child process.

.. important:: Don't Pass a MongoClient to a Child Process

If you use the ``fork()`` method to create a new process, don't pass an instance
of the ``MongoClient`` class from the parent process to the child process. This creates
a high probability of deadlock among ``MongoClient`` instances in the child process.
{+driver-short+} tries to issue a warning if this deadlock might occur.

Multiprocessing
~~~~~~~~~~~~~~~

{+driver-short+} supports the Python ``multiprocessing`` module.
However, on Unix systems, the multiprocessing module spawns processes by using
the ``fork()`` method. This carries the same risks described in :ref:`<pymongo-forks>`

To use multiprocessing with {+driver-short+}, write code similar to the following example:

.. code-block:: python

# Each process creates its own instance of MongoClient.
def func():
db = pymongo.MongoClient().mydb
# Do something with db.

proc = multiprocessing.Process(target=func)
proc.start()

.. important::

Do not copy an instance of the ``MongoClient`` class from the parent process to a child
process.

Type Hints
----------
Expand Down
73 changes: 72 additions & 1 deletion source/data-formats/extended-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,57 @@ list of dictionaries by using the ``loads()`` method:
{'bin': Binary(b'\x01\x02\x03\x04', 128)}
]

.. _pymongo-extended-json-binary-values:

Reading Binary Values in Python 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In Python 3, the driver decodes JSON binary values with subtype 0 to instances of the
``bytes`` class. In Python 2, the driver decodes these values to instances of the ``Binary``
class with subtype 0.

The following code examples show how {+driver-short+} decodes JSON binary instances with
subtype 0. Select the :guilabel:`Python 2` or :guilabel:`Python 3` tab to view the
corresponding code.

.. tabs::

.. tab:: Python 2
:tabid: python2

.. io-code-block::
:copyable: true

.. input::
:language: python

from bson.json_util import loads

doc = loads('{"b": {"$binary': b'this is a byte string'})
print(doc)

.. output::

{u'b': Binary('this is a byte string', 0)}

.. tab:: Python 3
:tabid: python3

.. io-code-block::
:copyable: true

.. input::
:language: python

from bson.json_util import loads

doc = loads('{"b": {"$binary': b'this is a byte string'})
print(doc)

.. output::

{'b': b'this is a byte string'}

Write Extended JSON
-------------------

Expand Down Expand Up @@ -273,10 +324,30 @@ The following example shows how to output Extended JSON in the Canonical format:
Additional Information
----------------------

The resources in the following sections provide more information about working
with Extended JSON.

API Documentation
~~~~~~~~~~~~~~~~~

For more information about the methods and types in ``bson.json_util``, see the following
API documentation:

- `loads() <{+api-root+}bson/json_util.html#bson.json_util.loads>`__
- `dumps() <{+api-root+}bson/json_util.html#bson.json_util.dumps>`__
- `CANONICAL_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.CANONICAL_JSON_OPTIONS>`__
- `LEGACY_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.LEGACY_JSON_OPTIONS>`__
- `LEGACY_JSON_OPTIONS <{+api-root+}bson/json_util.html#bson.json_util.LEGACY_JSON_OPTIONS>`__

Other Packages
~~~~~~~~~~~~~~

`python-bsonjs <https://pypi.python.org/pypi/python-bsonjs>`__ is another package,
built on top of `libbson <https://github.com/mongodb/libbson>`__,
that can convert BSON to Extended JSON. The ``python-bsonjs`` package doesn't
depend on {+driver-short+} and might offer a performance improvement over
``json_util`` in certain cases.

.. tip:: Use the RawBSONDocument Type

``python-bsonjs`` works best with {+driver-short+} when converting from the
``RawBSONDocument`` type.
Loading
Loading