|
| 1 | +.. _rust-runtimes: |
| 2 | + |
| 3 | +================================= |
| 4 | +Asynchronous and Synchronous APIs |
| 5 | +================================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn about the {+driver-short+}'s :ref:`asynchronous <rust-runtimes-configure-async>` |
| 17 | +and :ref:`synchronous <rust-runtimes-configure-sync>` APIs. |
| 18 | +This guide explains how to enable the available APIs and structure your code to use each. |
| 19 | + |
| 20 | +The {+driver-short+} supports ``tokio`` and ``async-std``, two popular asynchronous runtime crates. |
| 21 | +By default, the driver uses the ``tokio`` asynchronous runtime, but you can select a specific runtime |
| 22 | +by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file. |
| 23 | + |
| 24 | +The driver also includes a synchronous API for use cases that require blocking, or when parallelism is not necessary. |
| 25 | +You can select the synchronous API by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file. |
| 26 | + |
| 27 | +.. _rust-runtimes-configure-async: |
| 28 | + |
| 29 | +Configure the Asynchronous Runtime |
| 30 | +---------------------------------- |
| 31 | + |
| 32 | +The driver uses the ``tokio`` runtime by default. |
| 33 | +You can explicitly choose a runtime by adding the ``"tokio-runtime"`` or ``"async-std-runtime"`` |
| 34 | +feature flags to the ``mongodb`` dependency. |
| 35 | + |
| 36 | +Select from the following tabs to see how to add feature flags for each corresponding crate: |
| 37 | + |
| 38 | +.. tabs:: |
| 39 | + |
| 40 | + .. tab:: tokio |
| 41 | + :tabid: asynchronous-api-tokio |
| 42 | + |
| 43 | + .. code-block:: toml |
| 44 | + |
| 45 | + [dependencies.mongodb] |
| 46 | + version = "{+version+}" |
| 47 | + features = ["tokio-runtime"] |
| 48 | + |
| 49 | + .. tab:: async-std |
| 50 | + :tabid: asynchronous-api-async-std |
| 51 | + |
| 52 | + .. code-block:: toml |
| 53 | + |
| 54 | + [dependencies.mongodb] |
| 55 | + version = "{+version+}" |
| 56 | + default-features = false |
| 57 | + features = ["async-std"] |
| 58 | + |
| 59 | +For more information on installing the driver and adding feature flags, |
| 60 | +see the :ref:`rust-quick-start-download-and-install` step of the Quick Start. |
| 61 | + |
| 62 | +Tokio Runtime Example |
| 63 | +~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +The following code uses the ``task`` module from the ``tokio`` crate |
| 66 | +to create separate, concurrent tasks for multiple data operations: |
| 67 | + |
| 68 | +.. literalinclude:: /includes/fundamentals/code-snippets/runtimes-tokio.rs |
| 69 | + :language: rust |
| 70 | + :dedent: |
| 71 | + |
| 72 | +.. _rust-runtimes-configure-sync: |
| 73 | + |
| 74 | +Configure the Synchronous API |
| 75 | +----------------------------- |
| 76 | + |
| 77 | +The driver also provides a blocking, synchronous API. |
| 78 | +To use the synchronous API, add the either the ``"sync"`` or ``"tokio-sync"`` feature flag |
| 79 | +to the ``mongodb`` dependency. |
| 80 | + |
| 81 | +Select from the following tabs to see how to add feature flags for each corresponding crate: |
| 82 | + |
| 83 | +.. tabs:: |
| 84 | + |
| 85 | + .. tab:: sync |
| 86 | + :tabid: synchronous-api |
| 87 | + |
| 88 | + .. code-block:: toml |
| 89 | + |
| 90 | + [dependencies.mongodb] |
| 91 | + version = "{+version+}" |
| 92 | + default-features = false |
| 93 | + features = ["sync"] |
| 94 | + |
| 95 | + .. tab:: tokio-sync |
| 96 | + :tabid: synchronous-api-tokio |
| 97 | + |
| 98 | + .. code-block:: toml |
| 99 | + |
| 100 | + [dependencies.mongodb] |
| 101 | + version = "{+version+}" |
| 102 | + default-features = false |
| 103 | + features = ["tokio-sync"] |
| 104 | + |
| 105 | +Synchronous Code Example |
| 106 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 107 | + |
| 108 | +When using the synchronous API, use types from the ``mongodb::sync`` module to perform operations. |
| 109 | +The following code uses the ``sync`` module to insert data into a collection using the synchronous API. |
| 110 | +When the ``insert_one`` method runs inside the ``for`` loop, the driver waits for each request to complete before continuing. |
| 111 | + |
| 112 | +.. literalinclude:: /includes/fundamentals/code-snippets/runtimes-sync.rs |
| 113 | + :language: rust |
| 114 | + :dedent: |
| 115 | + |
| 116 | +Use Both Asynchronous and Synchronous APIs |
| 117 | +------------------------------------------ |
| 118 | + |
| 119 | +You can use both asynchronous and synchronous APIs in the same application. |
| 120 | +For example, to enable both ``tokio`` runtimes, you can add the ``tokio`` dependency to your dependencies list, and add the |
| 121 | +``"tokio-sync"`` flag to the ``mongodb`` dependency: |
| 122 | + |
| 123 | +.. code-block:: toml |
| 124 | + |
| 125 | + [dependencies] |
| 126 | + futures = "0.3.28" |
| 127 | + tokio = {version = "1.32.0", features = ["full"]} |
| 128 | + |
| 129 | + [dependencies.mongodb] |
| 130 | + version = "{+version+}" |
| 131 | + features = ["tokio-sync"] |
| 132 | + |
| 133 | +Additional Information |
| 134 | +---------------------- |
| 135 | + |
| 136 | +For more information about the concepts in this guide, see the following pages: |
| 137 | + |
| 138 | +- :ref:`Performance Considerations <rust-performance>` |
| 139 | +- `Asynchronous Programming in Rust <https://rust-lang.github.io/async-book/>`__ |
| 140 | + |
| 141 | +API Documentation |
| 142 | +~~~~~~~~~~~~~~~~~ |
| 143 | + |
| 144 | +To learn more about the methods and types discussed in this |
| 145 | +guide, see the following API Documentation: |
| 146 | + |
| 147 | +- `Client <{+api+}/struct.Client.html>`__ |
| 148 | +- `sync <{+api+}/sync/index.html>`__ |
0 commit comments