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

Update to pyo3 v0.14 #65

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
args: -i python --release --universal2 --out dist --no-sdist
- name: Install built wheel - universal2
run: |
pip install --force-reinstall dist/robyn*.whl
pip install --force-reinstall dist/robyn*_universal2.whl
cd ~ && python -c 'import robyn'
- name: Upload wheels
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -69,8 +69,9 @@ jobs:
target: ${{ matrix.target }}
args: -i python --release --out dist --no-sdist
- name: Install built wheel
sansyrox marked this conversation as resolved.
Show resolved Hide resolved
shell: bash
run: |
pip install robyn --no-index --find-links dist --force-reinstall
pip install --force-reinstall dist/robyn*.whl
cd ~ && python -c 'import robyn'
- name: Upload wheels
uses: actions/upload-artifact@v2
Expand Down
67 changes: 44 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ name = "robyn"
crate-type = ["cdylib", "rlib"]

[dependencies.pyo3]
version = "0.13.2"
version = "0.14.1"
features = ["extension-module"]

[dependencies.pyo3-asyncio]
version = "0.13.4"
features = []

[dependencies]
tokio = { version = "1.7.0", features = ["full"] }
dashmap = {git = "https://github.com/quake/dashmap", branch = "parking_lot", features = ["parking_lot"]}
pyo3-asyncio = { version="0.14.0" , features = ["attributes", "tokio-runtime"] }
anyhow = "1.0.38"
actix-web = "4.0.0-beta.8"
futures-util = "0.3.15"
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ pub fn start_server() {
}

#[pymodule]
pub fn robyn(py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn robyn(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
// the pymodule class to make the rustPyFunctions available
// in python
m.add_wrapped(wrap_pyfunction!(start_server))?;
m.add_class::<Server>()?;
pyo3_asyncio::try_init(py)?;
pyo3::prepare_freethreaded_python();
Ok(())
}
2 changes: 1 addition & 1 deletion src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async fn execute_function(
}
None => handler.call0(),
};
pyo3_asyncio::into_future(coro?)
pyo3_asyncio::tokio::into_future(coro?)
})?;
let output = output.await?;
let res = Python::with_gil(|py| -> PyResult<String> {
Expand Down
19 changes: 15 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use pyo3::prelude::*;
use pyo3::types::PyAny;

// hyper modules
use pyo3_asyncio::run_forever;

static STARTED: AtomicBool = AtomicBool::new(false);

#[pyclass]
Expand Down Expand Up @@ -44,16 +42,29 @@ impl Server {
let router = self.router.clone();
let headers = self.headers.clone();

let asyncio = py.import("asyncio").unwrap();

let event_loop = asyncio.call_method0("new_event_loop").unwrap();
asyncio
.call_method1("set_event_loop", (event_loop,))
.unwrap();
let event_loop_hdl = PyObject::from(event_loop);

thread::spawn(move || {
//init_current_thread_once();
actix_web::rt::System::new().block_on(async move {
let addr = format!("127.0.0.1:{}", port);

HttpServer::new(move || {
let event_loop_hdl = event_loop_hdl.clone();
App::new()
.app_data(web::Data::new(router.clone()))
.app_data(web::Data::new(headers.clone()))
.default_service(web::route().to(index))
.default_service(web::route().to(move |router, headers, payload, req| {
pyo3_asyncio::tokio::scope_local(event_loop_hdl.clone(), async move {
index(router, headers, payload, req).await
})
}))
})
.bind(addr)
.unwrap()
Expand All @@ -63,7 +74,7 @@ impl Server {
});
});

run_forever(py).unwrap()
event_loop.call_method0("run_forever").unwrap();
}

/// Adds a new header to our concurrent hashmap
Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def blocker():

if __name__ == "__main__":
app.add_header("server", "robyn")
app.start(port=5000)
app.start(port=5001)