Browse Source

Check if you can share a Runtime between threads

master
Julio Biason 3 years ago
parent
commit
957bb116b6
  1. 1059
      tokiosharedruntime/Cargo.lock
  2. 10
      tokiosharedruntime/Cargo.toml
  3. 55
      tokiosharedruntime/src/main.rs

1059
tokiosharedruntime/Cargo.lock generated

File diff suppressed because it is too large Load Diff

10
tokiosharedruntime/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "tokiosharedruntime"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sqlx = { version = "0.5.11", features = ["runtime-tokio-rustls", "sqlite", "macros"] }
tokio = { version = "1.17.0", features = ["rt"] }

55
tokiosharedruntime/src/main.rs

@ -0,0 +1,55 @@
use std::{sync::Arc, thread};
use sqlx::{sqlite::SqlitePoolOptions, Sqlite, SqlitePool};
use tokio::runtime::Runtime;
struct Connector {
runtime: Runtime,
pool: SqlitePool,
}
impl Connector {
pub fn new() -> Self {
let rt = Runtime::new().unwrap();
let conn = rt.block_on(async {
SqlitePoolOptions::new()
.max_connections(1)
.connect(":memory:")
.await
.expect("Failed to connect to memory")
});
Self {
runtime: rt,
pool: conn,
}
}
pub fn query(&self) -> i64 {
self.runtime.block_on(async {
let result: (i64,) = sqlx::query_as("SELECT 1")
.fetch_one(&self.pool)
.await
.unwrap();
result.0
})
}
}
fn main() {
let connector = Connector::new();
let shared = Arc::new(connector);
let internal1 = shared.clone();
let thread1 = thread::spawn(move || {
for _ in 0..5 {
println!("Thread 1: {}", internal1.query());
}
});
let thread2 = thread::spawn(move || {
for _ in 0..5 {
println!("Thread 2: {}", shared.query());
}
});
thread1.join().expect("Failed waiting for thread 1");
thread2.join().expect("Failed waiting for thread 2");
}
Loading…
Cancel
Save