Skip to content

Commit 60b735d

Browse files
committed
Replace some OnceCell with PyOnceLock
1 parent 5c7e8df commit 60b735d

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/lib.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,16 @@ pub mod doc_test {
397397
use std::future::Future;
398398

399399
use futures::channel::oneshot;
400-
use once_cell::sync::OnceCell;
401-
use pyo3::{call::PyCallArgs, prelude::*, types::PyDict};
400+
use pyo3::{call::PyCallArgs, prelude::*, sync::PyOnceLock, types::PyDict};
402401

403-
static ASYNCIO: OnceCell<Py<PyAny>> = OnceCell::new();
404-
static CONTEXTVARS: OnceCell<Py<PyAny>> = OnceCell::new();
405-
static ENSURE_FUTURE: OnceCell<Py<PyAny>> = OnceCell::new();
406-
static GET_RUNNING_LOOP: OnceCell<Py<PyAny>> = OnceCell::new();
402+
static ASYNCIO: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
403+
static CONTEXTVARS: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
404+
static ENSURE_FUTURE: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
405+
static GET_RUNNING_LOOP: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
407406

408407
fn ensure_future<'p>(py: Python<'p>, awaitable: &Bound<'p, PyAny>) -> PyResult<Bound<'p, PyAny>> {
409408
ENSURE_FUTURE
410-
.get_or_try_init(|| -> PyResult<Py<PyAny>> {
409+
.get_or_try_init(py, || -> PyResult<Py<PyAny>> {
411410
Ok(asyncio(py)?.getattr("ensure_future")?.into())
412411
})?
413412
.bind(py)
@@ -439,7 +438,7 @@ fn close(event_loop: Bound<PyAny>) -> PyResult<()> {
439438

440439
fn asyncio(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> {
441440
ASYNCIO
442-
.get_or_try_init(|| Ok(py.import("asyncio")?.into()))
441+
.get_or_try_init(py, || Ok(py.import("asyncio")?.into()))
443442
.map(|asyncio| asyncio.bind(py))
444443
}
445444

@@ -450,7 +449,7 @@ pub fn get_running_loop(py: Python) -> PyResult<Bound<PyAny>> {
450449
// Ideally should call get_running_loop, but calls get_event_loop for compatibility when
451450
// get_running_loop is not available.
452451
GET_RUNNING_LOOP
453-
.get_or_try_init(|| -> PyResult<Py<PyAny>> {
452+
.get_or_try_init(py, || -> PyResult<Py<PyAny>> {
454453
let asyncio = asyncio(py)?;
455454

456455
Ok(asyncio.getattr("get_running_loop")?.into())
@@ -461,7 +460,7 @@ pub fn get_running_loop(py: Python) -> PyResult<Bound<PyAny>> {
461460

462461
fn contextvars(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> {
463462
Ok(CONTEXTVARS
464-
.get_or_try_init(|| py.import("contextvars").map(|m| m.into()))?
463+
.get_or_try_init(py, || py.import("contextvars").map(|m| m.into()))?
465464
.bind(py))
466465
}
467466

0 commit comments

Comments
 (0)