Skip to content

Commit 1f1f7b4

Browse files
authored
RUST-329 Update multi-threaded examples to use tasks (#170)
1 parent e782397 commit 1f1f7b4

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/client/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ const DEFAULT_SERVER_SELECTION_TIMEOUT: Duration = Duration::from_secs(30);
3434
/// as servers being added or removed
3535
///
3636
/// `Client` uses [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) internally,
37-
/// so it can safely be shared across threads. For example:
37+
/// so it can safely be shared across threads or async tasks. For example:
3838
///
3939
/// ```rust
4040
/// # #[cfg(not(feature = "sync"))]
4141
/// # use mongodb::{Client, error::Result};
42+
/// # #[cfg(feature = "async-std-runtime")]
43+
/// # use async_std::task;
44+
/// # #[cfg(feature = "tokio-runtime")]
45+
/// # use tokio::task;
4246
/// #
4347
/// # #[cfg(not(feature = "sync"))]
4448
/// # async fn start_workers() -> Result<()> {
@@ -47,16 +51,13 @@ const DEFAULT_SERVER_SELECTION_TIMEOUT: Duration = Duration::from_secs(30);
4751
/// for i in 0..5 {
4852
/// let client_ref = client.clone();
4953
///
50-
/// std::thread::spawn(move || {
54+
/// task::spawn(async move {
5155
/// let collection = client_ref.database("items").collection(&format!("coll{}", i));
5256
///
5357
/// // Do something with the collection
5458
/// });
5559
/// }
5660
/// #
57-
/// # // Technically we should join the threads here, but for the purpose of the example, we'll just
58-
/// # // sleep for a bit.
59-
/// # std::thread::sleep(std::time::Duration::from_secs(3));
6061
/// # Ok(())
6162
/// # }
6263
/// ```

src/coll/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ const MAX_INSERT_DOCS_BYTES: usize = 16 * 1000 * 1000;
4242
/// [`Database::collection_with_options`](struct.Database.html#method.collection_with_options).
4343
///
4444
/// `Collection` uses [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) internally,
45-
/// so it can safely be shared across threads. For example:
45+
/// so it can safely be shared across threads or async tasks. For example:
4646
///
4747
/// ```rust
4848
/// # use bson::{bson, doc};
4949
/// # use mongodb::error::Result;
50+
/// # #[cfg(feature = "async-std-runtime")]
51+
/// # use async_std::task;
52+
/// # #[cfg(feature = "tokio-runtime")]
53+
/// # use tokio::task;
5054
/// #
5155
/// # #[cfg(not(feature = "sync"))]
5256
/// # async fn start_workers() -> Result<()> {
@@ -58,15 +62,12 @@ const MAX_INSERT_DOCS_BYTES: usize = 16 * 1000 * 1000;
5862
/// for i in 0..5 {
5963
/// let coll_ref = coll.clone();
6064
///
61-
/// std::thread::spawn(move || {
65+
/// task::spawn(async move {
6266
/// // Perform operations with `coll_ref`. For example:
63-
/// coll_ref.insert_one(doc! { "x": i }, None);
67+
/// coll_ref.insert_one(doc! { "x": i }, None).await;
6468
/// });
6569
/// }
6670
/// #
67-
/// # // Technically we should join the threads here, but for the purpose of the example, we'll just
68-
/// # // sleep for a bit.
69-
/// # std::thread::sleep(std::time::Duration::from_secs(3));
7071
/// # Ok(())
7172
/// # }
7273
/// ```

src/db/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ use crate::{
3131
/// [`Client::database_with_options`](struct.Client.html#method.database_with_options).
3232
///
3333
/// `Database` uses [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) internally,
34-
/// so it can safely be shared across threads. For example:
34+
/// so it can safely be shared across threads or async tasks. For example:
3535
///
3636
/// ```rust
3737
///
3838
/// # #[cfg(not(feature = "sync"))]
3939
/// # use mongodb::{Client, error::Result};
40+
/// # #[cfg(feature = "async-std-runtime")]
41+
/// # use async_std::task;
42+
/// # #[cfg(feature = "tokio-runtime")]
43+
/// # use tokio::task;
44+
/// #
4045
/// #
4146
/// # #[cfg(not(feature = "sync"))]
4247
/// # async fn start_workers() -> Result<()> {
@@ -46,16 +51,13 @@ use crate::{
4651
/// for i in 0..5 {
4752
/// let db_ref = db.clone();
4853
///
49-
/// std::thread::spawn(move || {
54+
/// task::spawn(async move {
5055
/// let collection = db_ref.collection(&format!("coll{}", i));
5156
///
5257
/// // Do something with the collection
5358
/// });
5459
/// }
5560
/// #
56-
/// # // Technically we should join the threads here, but for the purpose of the example, we'll just
57-
/// # // sleep for a bit.
58-
/// # std::thread::sleep(std::time::Duration::from_secs(3));
5961
/// # Ok(())
6062
/// # }
6163
/// ```

0 commit comments

Comments
 (0)