Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
victorteokw committed Jan 28, 2024
1 parent f5e1cfc commit 6e31661
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/handler/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 1 addition & 8 deletions src/namespace/namespace.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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::<PyObject, PyErr>(response.into_py(py))
})
})())?;
Ok::<PyObject, PyErr>(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::<PyObject, teo::prelude::Error>(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)
Expand Down
40 changes: 0 additions & 40 deletions src/utils/await_coroutine_if_needed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<PyObject> {
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)
})
}

0 comments on commit 6e31661

Please sign in to comment.