Skip to content
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

Add Event handlers #154

Merged
merged 6 commits into from
Jan 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup handler
  • Loading branch information
sansyrox committed Jan 16, 2022
commit 5f97e894b8ecbbf72c5b04b37b48e6298fafc004
16 changes: 13 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl Server {
.call_method1("set_event_loop", (event_loop,))
.unwrap();
let event_loop_hdl = PyObject::from(event_loop);
let event_loop_cleanup = PyObject::from(event_loop);
let startup_handler = self.startup_handler.clone();
let shutdown_handler = self.shutdown_handler.clone();

Expand Down Expand Up @@ -162,12 +163,21 @@ impl Server {
.run()
.await
.unwrap();

execute_event_handler(shutdown_handler, event_loop_hdl.clone()).await;
});
});

event_loop.call_method0("run_forever").unwrap();
let event_loop = event_loop.call_method0("run_forever");
if event_loop.is_err() {
println!("Ctrl c handler");
Python::with_gil(|py| {
let event_loop_hdl = event_loop_cleanup.clone();
pyo3_asyncio::tokio::run(py, async move {
execute_event_handler(shutdown_handler, event_loop_hdl.clone()).await;
Ok(())
})
.unwrap();
})
}
Ok(())
}

Expand Down