diff --git a/src/dynamic/mod.rs b/src/dynamic/mod.rs index 55ffd9c..ac5a3d6 100644 --- a/src/dynamic/mod.rs +++ b/src/dynamic/mod.rs @@ -16,7 +16,7 @@ use crate::object::model::teo_model_object_to_py_any; use crate::object::py_any_to_teo_object; use crate::object::value::{teo_value_to_py_any, py_any_to_teo_value}; use crate::result::{IntoPyResultWithGil, IntoTeoPathResult}; -use crate::utils::await_coroutine_if_needed::{await_coroutine_if_needed, await_coroutine_if_needed_async, await_coroutine_if_needed_async_value}; +use crate::utils::await_coroutine_if_needed::await_coroutine_if_needed_async_value; use crate::utils::check_py_dict::check_py_dict; use self::model_ctx_wrapper::ModelCtxWrapper; diff --git a/src/handler/group.rs b/src/handler/group.rs index 83cb8be..d691819 100644 --- a/src/handler/group.rs +++ b/src/handler/group.rs @@ -5,7 +5,7 @@ use crate::dynamic::py_ctx_object_from_teo_transaction_ctx; use crate::object::value::teo_value_to_py_any; use crate::request::request::Request; use crate::response::Response; -use crate::utils::await_coroutine_if_needed::{await_coroutine_if_needed, await_coroutine_if_needed_async_value}; +use crate::utils::await_coroutine_if_needed::await_coroutine_if_needed_async_value; use crate::utils::check_callable::check_callable; use crate::result::IntoTeoPathResult; diff --git a/src/namespace/namespace.rs b/src/namespace/namespace.rs index 5dcf169..bfdc6b0 100644 --- a/src/namespace/namespace.rs +++ b/src/namespace/namespace.rs @@ -1,7 +1,7 @@ use pyo3::{pyclass, pymethods, types::PyCFunction, IntoPy, Py, PyErr, PyObject, PyResult, Python}; use teo::prelude::{handler::Group as TeoHandlerGroup, model::Field as TeoField, model::Property as TeoProperty, model::Relation as TeoRelation, request, Enum as TeoEnum, Member as TeoEnumMember, Middleware, Model as TeoModel, Namespace as TeoNamespace, Next}; -use crate::{utils::{check_callable::check_callable, await_coroutine_if_needed::{await_coroutine_if_needed, await_coroutine_if_needed_async_value}}, object::{arguments::teo_args_to_py_args, value::teo_value_to_py_any}, model::{model::Model, field::field::Field, relation::relation::Relation, property::property::Property}, result::{IntoPyResultWithGil, IntoTeoPathResult, IntoTeoResult}, r#enum::{r#enum::Enum, member::member::EnumMember}, request::{Request, RequestCtx}, dynamic::{py_ctx_object_from_teo_transaction_ctx, teo_transaction_ctx_from_py_ctx_object}, response::Response, handler::group::HandlerGroup}; +use crate::{utils::{check_callable::check_callable, await_coroutine_if_needed::await_coroutine_if_needed_async_value}, object::{arguments::teo_args_to_py_args, value::teo_value_to_py_any}, model::{model::Model, field::field::Field, relation::relation::Relation, property::property::Property}, result::{IntoPyResultWithGil, IntoTeoPathResult, IntoTeoResult}, r#enum::{r#enum::Enum, member::member::EnumMember}, request::{Request, RequestCtx}, dynamic::py_ctx_object_from_teo_transaction_ctx, response::Response, handler::group::HandlerGroup}; #[pyclass] pub struct Namespace { @@ -217,28 +217,21 @@ impl Namespace { let ctx: RequestCtx = args.get_item(0)?.extract()?; let teo_ctx = ctx.teo_inner.clone(); let coroutine = pyo3_asyncio::tokio::future_into_py_with_locals::<_, PyObject>(py, main_thread_locals.clone(), (|| async { - println!("here runs into 1"); let result: teo::prelude::Response = next.call(teo_ctx).await.unwrap(); - println!("here runs into 2"); Python::with_gil(|py| { - println!("here runs into 3"); let response = Response { teo_response: result }; - println!("here runs into 4"); Ok::(response.into_py(py)) }) })())?; Ok::(coroutine.into_py(py)) }) }).unwrap(); - println!("here runs start 0"); let coroutine = shared_result_function.call1(py, (py_ctx, py_next)).into_teo_result()?; Ok::(coroutine.into_py(py)) })?; - println!("here runs start 1"); let result = await_coroutine_if_needed_async_value(coroutine, main_thread_locals).await.into_teo_result()?; - println!("here runs end 0"); Python::with_gil(|py| { let response: Response = result.extract(py).into_teo_result()?; Ok(response.teo_response) diff --git a/src/utils/await_coroutine_if_needed.rs b/src/utils/await_coroutine_if_needed.rs index 972cb6e..013b394 100644 --- a/src/utils/await_coroutine_if_needed.rs +++ b/src/utils/await_coroutine_if_needed.rs @@ -12,22 +12,6 @@ pub async fn await_coroutine_if_needed_async_value(transformed_py: PyObject, mai Ok::<_, PyErr>(f) })?; f.await - // if let Some(event_loop) = check_event_loop()? { - // Python::with_gil(|py| { - // let f = into_future_with_locals(&TaskLocals::new(event_loop.as_ref(py)), transformed_py.as_ref(py))?; - // pyo3_asyncio::tokio::run_until_complete(event_loop.as_ref(py), async move { - // let result = f.await; - // result - // }) - // }) - // } else { - // let f = Python::with_gil(|py| { - // let event_loop = pyo3_asyncio::tokio::get_current_loop(py)?; - // let f = into_future_with_locals(&TaskLocals::new(event_loop), transformed_py.as_ref(py))?; - // Ok::<_, PyErr>(f) - // })?; - // f.await - // } } else { Ok(Python::with_gil(|py| { transformed_py.into_py(py) @@ -45,27 +29,3 @@ pub async fn await_coroutine_if_needed_async(transformed_py: &PyAny) -> PyResult })) } } - -pub fn await_coroutine_if_needed(py: Python<'_>, transformed_py: &PyAny) -> PyResult { - Ok(if is_coroutine(transformed_py)? { - println!("is coroutine"); - let asyncio = py.import("asyncio")?; - let event_loop = asyncio.call_method0("get_event_loop").unwrap_or( - { - let event_loop = asyncio.call_method0("new_event_loop")?; - asyncio.call_method1("set_event_loop", (event_loop,))?; - event_loop - } - ); - let f = into_future_with_locals(&TaskLocals::new(event_loop), transformed_py)?; - pyo3_asyncio::tokio::run_until_complete(event_loop, async move { - println!("run until complete"); - let result = f.await; - println!("run until complete done"); - result - })? - } else { - println!("is not coroutine"); - transformed_py.into_py(py) - }) -} \ No newline at end of file