錆びた非同期関数を Python にエクスポートしようとしています。
そして、これが私のpythonファイルで使用したい方法です。
from rust_lib import get_weather
async def call_me():
weather = await get_weather()
print(weather.current_tmp)
Rustでは、私は好きでした
#[pyclass]
#[derive(Debug, Default)]
struct Weather {
#[pyo3(get)]
current_tmp: String,
}
async fn get_weather() -> Result<Weather, reqwest::Error> {
let client = reqwest::Client::builder()
.danger_accept_invalid_certs(true)
.connect_timeout(Duration::from_secs(2))
.build()?;
let res = client.get(url).header(USER_AGENT, "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36").send().await?;
let body = res.text().await?;
// HTML Parse
let mut weather: Weather = Default::default();
let document = Html::parse_document(&body);
// some parsing
Ok(weather)
}
しかし、ここでこのエラーが発生しますfuture cannot be sent between threads safely
#[pyfunction]
fn get_weather(py: Python) -> PyResult<&PyAny> {
pyo3_asyncio::tokio::future_into_py(py, async move {
let weather = weather_parse().await.unwrap().into_py(py);
Ok(weather)
})
}
ここに長い間立ち往生していて、自分が何をしているのかさえわかりません..
前もって感謝します!