-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Python] Allow to run pure Python request middlewares inside a Tower service #1734
Changes from 33 commits
d1d74fe
da9e052
aeeaa89
e20563e
583c967
047401c
88bbd11
aa2dc0a
5e784f6
03c660b
28e3c09
f3e21de
84cc06b
78a6b6f
38d971c
cf7521d
80cdbfc
e79797f
b08d331
959b02c
8415503
37036b8
1d51bb8
2a00ec2
5e6bfe2
c3c2d65
e75d2db
1cddc95
324e30f
34c55f1
fb58bdf
e06d7d7
8f98141
964a470
4ee2743
b9a8e3b
433c162
747b6c0
da065a4
6c1f9ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,11 +69,20 @@ class PythonServerOperationHandlerGenerator( | |
handler: #{SmithyPython}::PyHandler, | ||
) -> std::result::Result<$output, $error> { | ||
// Async block used to run the handler and catch any Python error. | ||
let span = #{tracing}::span!( | ||
#{tracing}::Level::TRACE, "python", | ||
pid = #{tracing}::field::Empty, | ||
module = #{tracing}::field::Empty, | ||
filename = #{tracing}::field::Empty, | ||
lineno = #{tracing}::field::Empty | ||
); | ||
let guard = span.enter(); | ||
let result = if handler.is_coroutine { | ||
#{PyCoroutine:W} | ||
crisidev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
#{PyFunction:W} | ||
}; | ||
drop(guard); | ||
#{PyError:W} | ||
} | ||
""", | ||
|
@@ -90,16 +99,14 @@ class PythonServerOperationHandlerGenerator( | |
rustTemplate( | ||
""" | ||
#{tracing}::debug!("Executing Python handler function `$name()`"); | ||
#{tokio}::task::block_in_place(move || { | ||
#{pyo3}::Python::with_gil(|py| { | ||
let pyhandler: &#{pyo3}::types::PyFunction = handler.extract(py)?; | ||
let output = if handler.args == 1 { | ||
pyhandler.call1((input,))? | ||
} else { | ||
pyhandler.call1((input, state.0))? | ||
}; | ||
output.extract::<$output>() | ||
}) | ||
#{pyo3}::Python::with_gil(|py| { | ||
let pyhandler: &#{pyo3}::types::PyFunction = handler.extract(py)?; | ||
let output = if handler.args == 1 { | ||
pyhandler.call1((input,))? | ||
} else { | ||
pyhandler.call1((input, state.0))? | ||
}; | ||
output.extract::<$output>() | ||
Comment on lines
+93
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for removing No matter what we do, this synchronous mutex will not allow tokio to continue the execution. Removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other frameworks using the same approach as Smithy-rs Python are also doing the same: sparckles/Robyn@4ef01e6 |
||
}) | ||
""", | ||
*codegenScope, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is handy if you prefer this style https://docs.rs/tracing/latest/tracing/struct.Span.html#method.entered