小编Yan*_*Liu的帖子

如何在 Erlang 中进行并行调用并等待所有结果?

我正在 Erlang 开发手机游戏后端。对于每个 HTTP 请求,它可能需要查询不同的数据源,例如 PostgreSQL、MongoDB 和 Redis。我想并行地对这些数据源进行独立调用,但找不到明确的 Erlang 方法。

例如,

handle_request(?POST, <<"login">>, UserId, Token) ->
    % Verify token from PostgreSQL
    AuthResult = auth_service:login(UserId, Token), 

    % Get user data such as outfits and level from MongoDB
    UserDataResult = userdata_service:get(UserId),

    % Get messages sent to the user from Redis
    MessageResult = message_service:get(UserId),

    % How to run the above 3 calls in parallel?
    % Then wait for all their results here? 

    % Combine the result and send back to client
    build_response(AuthResult, UserDataResult, …
Run Code Online (Sandbox Code Playgroud)

erlang asynchronous

6
推荐指数
1
解决办法
522
查看次数

标签 统计

asynchronous ×1

erlang ×1