-
-
Save rust-play/2168724b061bf6b8aa2f2d3c64bb235b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use async_std::sync::{Mutex, MutexGuard}; | |
use pyo3::prelude::*; | |
struct AsyncPyo3 { | |
inner: Mutex<()>, | |
} | |
struct AsyncGILGuard<'a> { | |
_guard: MutexGuard<'a, ()>, | |
gil: GILGuard, | |
} | |
impl AsyncPyo3 { | |
fn new() -> AsyncPyo3 { | |
AsyncPyo3 { | |
inner: Mutex::new(()), | |
} | |
} | |
async fn acquire_gil(&self) -> AsyncGILGuard<'_> { | |
let _guard = self.inner.lock().await; | |
let gil = Python::acquire_gil(); | |
AsyncGILGuard { _guard, gil } | |
} | |
} | |
impl<'a> AsyncGILGuard<'a> { | |
fn python(&self) -> Python { | |
self.gil.python() | |
} | |
} | |
lazy_static::lazy_static! { | |
static ref PYO3: AsyncPyo3 = AsyncPyo3::new(); | |
} | |
#[async_std::main] | |
async fn main() { | |
pyo3::prepare_freethreaded_python(); | |
let gil = PYO3.acquire_gil().await; | |
let py = gil.python(); | |
let msg = "Hello World!"; | |
use pyo3::py_run; | |
py_run!(py, msg, "print(msg)"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment