She*_*Lei 2 python asynchronous rust python-cffi
我将reqwestrust 的一些函数包装到req.lib文件中,并使用cffi. 然而reqwest::blocking::Client强迫我在 python 中使用多线程。我发现reqwest可以在 Rust 中以异步模式调用。我想知道有没有办法实现req.lib异步?即使是半异步对我来说也可以。
例如,当前存根签名是:
#[no_mangle]
pub extern "C" fn urlopen(url: *const c_char) -> *mut c_char
Run Code Online (Sandbox Code Playgroud)
我可以写一些类似的东西:
#[no_mangle]
pub extern "C" fn urlopen(url: *const c_char) -> u64 // return request unique id
#[no_mangle]
pub extern "C" fn is_finished(req_id: u64) -> bool // whether given request is done
#[no_mangle]
pub extern "C" fn fetch_result(req_id: u64) -> *mut c_char // fetch response
Run Code Online (Sandbox Code Playgroud)
因此cffi调用不再锁定主线程。我可以使用单线程来调用多个请求。欢迎任何建议或最佳实践。
异步代码是通过特殊的运行时执行的,对于 python 和 rust 来说,这些是不同且不兼容的库。在那里,你不能简单地在语言之间共享 future,它必须以创建它的相同语言运行。
至于您的示例,这意味着您需要运行一个Clientin rust 执行器(例如在 tokio 中),然后从中获得反馈。作为最简单的方法,您可以创建一个全局方法:
use lazy_static::lazy_static;
use tokio::runtime::Runtime;
lazy_static! {
static ref RUNTIME: Runtime = Runtime::new().unwrap();
}
Run Code Online (Sandbox Code Playgroud)
然后,在生成后,您需要获得反馈,因此您可以使用一些带有状态和结果的地图:
use std::collections::HashMap;
use std::sync::RwLock;
use futures::prelude::*;
use tokio::sync::oneshot;
type FutureId = u64;
type UrlResult = reqwest::Result<String>;
type SyncMap<K, V> = RwLock<HashMap<K, V>>;
lazy_static! {
// Map for feedback channels. Once result is computed, it is stored at `RESULTS`
static ref STATUSES: SyncMap<FutureId, oneshot::Receiver<UrlResult>> = SyncMap::default();
// Cache storage for results
static ref RESULTS: SyncMap<FutureId, UrlResult> = SyncMap::default();
}
fn gen_unique_id() -> u64 { .. }
#[no_mangle]
pub extern "C" fn urlopen(url: *const c_char) -> FutureId {
let url: &str = /* convert url */;
let (tx, rx) = oneshot::channel();
RUNTIME.spawn(async move {
let body = reqwest::get(url).and_then(|b| b.text()).await;
tx.send(body).unwrap(); // <- this one should be handled somehow
});
let id = gen_unique_id();
STATUSES.write().unwrap().insert(id, rx);
id
}
Run Code Online (Sandbox Code Playgroud)
这里,对于每个urlopen请求oneshot::channel都在创建,这就延迟了执行结果。因此可以检查它是否完成:
#[no_mangle]
pub extern "C" fn is_finished(req_id: u64) -> bool {
// first check in cache
if RESULTS.read().unwrap().contains_key(&req_id) {
true
} else {
let mut res = RESULTS.write().unwrap();
let mut statuses = STATUSES.write().unwrap();
// if nothing in cache, check the feedback channel
if let Some(rx) = statuses.get_mut(&req_id) {
let val = match rx.try_recv() {
Ok(val) => val,
Err(_) => {
// handle error somehow here
return true;
}
};
// and cache the result, if available
res.insert(req_id, val);
true
} else {
// Unknown request id
true
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么获取结果就相当简单了:
#[no_mangle]
pub extern "C" fn fetch_result(req_id: u64) -> *const c_char {
let res = RESULTS.read().unwrap();
res.get(&req_id)
// there `ok()` should probably be handled in some better way
.and_then(|val| val.as_ref().ok())
.map(|val| val.as_ptr())
.unwrap_or(std::ptr::null()) as *const _
}
Run Code Online (Sandbox Code Playgroud)
游乐场链接。
请记住,上述解决方案有其优点:
以及显着的缺点:
RESULTS无限期地增长并且永远不会被清除;thread_local!可以用于全局而不是锁;STATUSES在is_finished获取写访问权限时,虽然可能最好先获得读访问权限;