Struct aws_smithy_http_server_python::PyApp
source · [−]pub struct PyApp {
pub handlers: PyHandlers,
pub context: Option<Arc<PyObject>>,
pub workers: Mutex<Vec<PyObject>>,
pub router: Option<PyRouter>,
}
Expand description
Python application definition, holding the handlers map, the optional Python context object, the list of workers and the PyRouter.
Fields
handlers: PyHandlers
context: Option<Arc<PyObject>>
workers: Mutex<Vec<PyObject>>
router: Option<PyRouter>
Implementations
sourceimpl PyApp
impl PyApp
sourcepub fn context(&mut self, _py: Python<'_>, context: PyObject)
pub fn context(&mut self, _py: Python<'_>, context: PyObject)
Register a new context object inside the Rust state.
sourcepub fn start_worker(
&mut self,
py: Python<'_>,
socket: &PyCell<PySocket>,
worker_number: isize
) -> PyResult<()>
pub fn start_worker(
&mut self,
py: Python<'_>,
socket: &PyCell<PySocket>,
worker_number: isize
) -> PyResult<()>
Start a single worker with its own Tokio and Python async runtime and provided shared socket.
Python asynchronous loop needs to be started and handled during the lifetime of the process.
First of all we install uvloop as the main Python event loop. Thanks to libuv, uvloop
performs ~20% better than Python standard event loop in most benchmarks, while being 100%
compatible.
We retrieve the Python context object, if setup by the user calling PyApp::context method,
generate the PyState structure and build the aws_smithy_http_server::Router, filling
it with the functions generated by PythonServerOperationHandlerGenerator.kt
.
At last we get a cloned reference to the underlying socket2::Socket.
Now that all the setup is done, we can start the two runtimes and run the hyper server.
We spawn a thread with a new tokio::runtime, setup the middlewares and finally block the
thread on hyper::serve
.
The main process continues and at the end it is blocked on Python loop.run_forever()
.
sourcepub fn register_operation(
&mut self,
py: Python<'_>,
name: &str,
func: PyObject
) -> PyResult<()>
pub fn register_operation(
&mut self,
py: Python<'_>,
name: &str,
func: PyObject
) -> PyResult<()>
Register a new operation in the handlers map.
The operation registered in the map are used inside the code-generated router()
method
and passed to the aws_smithy_http_server::Router as part of the operation handlers call.
sourcepub fn run(
&mut self,
py: Python<'_>,
address: Option<String>,
port: Option<i32>,
backlog: Option<i32>,
workers: Option<usize>
) -> PyResult<()>
pub fn run(
&mut self,
py: Python<'_>,
address: Option<String>,
port: Option<i32>,
backlog: Option<i32>,
workers: Option<usize>
) -> PyResult<()>
Main entrypoint: start the server on multiple workers.
The multiprocessing server is achieved using the ability of a Python interpreter
to clone and start itself as a new process.
The shared sockets is created and Using the multiprocessing::Process module, multiple
workers with the method self.start_worker()
as target are started.
Trait Implementations
sourceimpl PyClass for PyApp
impl PyClass for PyApp
type Dict = PyClassDummySlot
type Dict = PyClassDummySlot
Specify this class has #[pyclass(dict)]
or not.
type WeakRef = PyClassDummySlot
type WeakRef = PyClassDummySlot
Specify this class has #[pyclass(weakref)]
or not.
type BaseNativeType = PyAny
type BaseNativeType = PyAny
The closest native ancestor. This is PyAny
by default, and when you declare
#[pyclass(extends=PyDict)]
, it’s PyDict
. Read more
sourceimpl PyClassImpl for PyApp
impl PyClassImpl for PyApp
sourceconst IS_BASETYPE: bool = true
const IS_BASETYPE: bool = true
#[pyclass(subclass)]
sourceconst IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
#[pyclass(extends=…)]
sourceconst IS_MAPPING: bool = false
const IS_MAPPING: bool = false
#[pyclass(mapping)]
type ThreadChecker = ThreadCheckerStub<PyApp>
type ThreadChecker = ThreadCheckerStub<PyApp>
This handles following two situations: Read more
fn for_all_items(visitor: &mut dyn FnMut(&PyClassItems))
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
sourceimpl PyMethods<PyApp> for PyClassImplCollector<PyApp>
impl PyMethods<PyApp> for PyClassImplCollector<PyApp>
fn py_methods(self) -> &'static PyClassItems
sourceimpl PyTypeInfo for PyApp
impl PyTypeInfo for PyApp
type AsRefTarget = PyCell<PyApp>
type AsRefTarget = PyCell<PyApp>
Utility type to make Py::as_ref work.
sourcefn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
PyTypeObject instance for this type.
sourcefn is_type_of(object: &PyAny) -> bool
fn is_type_of(object: &PyAny) -> bool
Checks if object
is an instance of this type or a subclass of this type.
sourcefn is_exact_type_of(object: &PyAny) -> bool
fn is_exact_type_of(object: &PyAny) -> bool
Checks if object
is an instance of this type.
Auto Trait Implementations
impl !RefUnwindSafe for PyApp
impl Send for PyApp
impl !Sync for PyApp
impl Unpin for PyApp
impl !UnwindSafe for PyApp
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<'a, T> FromPyObject<'a> for T where
T: PyClass + Clone,
impl<'a, T> FromPyObject<'a> for T where
T: PyClass + Clone,
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> PyTypeObject for T where
T: PyTypeInfo,
impl<T> PyTypeObject for T where
T: PyTypeInfo,
sourcefn type_object(py: Python<'_>) -> &PyType
fn type_object(py: Python<'_>) -> &PyType
Returns the safe abstraction over the type object.
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more