Open xMaxximum opened 1 year ago
For clarification, I merely copied from the tutorial, am I doing something wrong?
You have to be consistent with your runtimes. I think the libraries you are trying to use are built to work with tokio, but you are using the async-std runtime.
You'll either have to use the tokio example code or use a tokio compatibility feature in async-std.
Could you maybe rewrite my code quickly so it uses tokio? I tried a plethora of different ways and kept getting the same error...
Ah yep, you're right. Apparently this yahoo finance library is pretty old, so it's using a 0.2 version of tokio's API. Tokio's runtime doesn't support these older libraries out of the box.
Luckily however, there's a pretty nice solution to this. You can wrap your future in a tokio_compat_02
compatibility layer, and then it should work!
Cargo.toml
[lib]
name = "yahoofetcher"
crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures = "0.3.28"
pyo3 = { version = "0.19", features = ["extension-module"] }
# Added tokio-runtime feature
pyo3-asyncio = { version = "0.19", features = ["tokio-runtime"] }
reqwest = "0.11.18"
tokio = { version = "1.29.1", features = ["full"]}
# Added tokio-compat-02 crate
tokio-compat-02 = "0.2.0"
yahoo-finance = "0.3.0"
lib.rs
use pyo3::prelude::*;
// import the FutureExt trait to enable .compat() usage
use tokio_compat_02::FutureExt;
use yahoo_finance::{history, Interval, Timestamped};
#[pyfunction]
fn call_test(py: Python<'_>) -> PyResult<&PyAny> {
// Changed to pyo3_asyncio::tokio::future_into_py
pyo3_asyncio::tokio::future_into_py(py, async move {
test().await;
Ok(Python::with_gil(|py| py.None()))
})
}
async fn test() {
// Added a .compat() to convert the future into a tokio 0.2-compatible future
let data = history::retrieve_interval("AAPL", Interval::_6mo)
.compat()
.await
.unwrap();
for bar in &data {
println!(
"Apple hit an intraday high of ${:.2} on {}.",
bar.high,
bar.datetime().format("%b %e %Y")
);
}
}
/// A Python module implemented in Rust.
#[pymodule]
fn yahoofetcher(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(call_test, m)?)?;
Ok(())
}
Basically, any time the yahoo finance library gives you a future to work with, you first call .compat()
on it to allow it to work with the modern Tokio API.
I also updated pyo3-asyncio to 0.19 since it's out now.
LMK if this works for you!
Hey,
Sorry for the late response, I was busy with work...
Yes, this does work! Unfortunately I'll have to abandon this though as the yahoo finance library isn't really up-to-date anymore and the only option would be writing something myself - I'll have to think about that
Anyways, thank you again!
π Bug Reports
When reporting a bug, please provide the following information. If this is not a bug report you can just discard this template.
π Environment
rustc --version
): 1.64.0version = "0.x.y"
withgit = "https://github.com/awestlake87/pyo3-asyncio")?
: yepπ₯ Reproducing
Please provide a minimal working example. This means both the Rust code and the Python.
Please also write what exact flags are required to reproduce your results.
toml:
rust:
py:
I get this error:
any help would be appreciated, thanks!