简体   繁体   English

如何在 Lambda 处理程序中处理多个异步函数?

[英]How to handle multiple async functions in a Lambda handler?

I have the following code:我有以下代码:

async fn function_handler(_event: Request) -> Result<Response<Body>, Error> {

    let db_eid_cache = create_db_eid_cache();
    let table_eid_cache = create_table_eid_cache();

    try_join!(db_eid_cache, table_eid_cache);

    Ok(Response::builder()
        .status(200)
        .header("content-type", "application/json")
        .body("ok".into())
        .map_err(Box::new)?)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_target(false)
        .without_time()
        .init();

    run(service_fn(function_handler)).await
}

This does not compile:这不编译:

future cannot be sent between threads safely
the trait `std::marker::Send` is not implemented for `(dyn StdError + 'static)`rustcClick for full compiler diagnostic
join_mod.rs(105, 13): future is not `Send` as this value is used across an await
join_mod.rs(107, 13): the value is later dropped here
lib.rs(185, 16): required by a bound in `lambda_http::run`

I was trying to add Send for the function_handler() return type but it does not work either.我试图为 function_handler() 返回类型添加发送,但它也不起作用。

Adding + Send + Sync to all functions that are called by either of the functions that I am joining fixes the problem.将 + Send + Sync 添加到由我加入的任一函数调用的所有函数可以解决问题。

Result<String, Box<dyn error::Error + Send + Sync>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM