Debian Codegraph @

demo/server/handlerAsyncMethod.cpp

raw
1 /*
2 * Copyright (C) 2024 KylinSoft Co., Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 3 of the License, or (at your option) any later
7 * version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <unistd.h>
18
19 #include <thread>
20
21 #include "scene.h"
22
23 void workMethod2(const std::string &taskId) {
24 PRINT_INFO("workMethod2 start, input: %s.\n", taskId.c_str());
25 sleep(10);
26 Response output = Response::success();
27 output.add("p1", 11);
28 output.add("p2", "hehe");
29 ASYNC_RESPONSE(taskId, output);
30 PRINT_INFO("workMethod2 end.\n");
31 }
32
33 Response handleAsyncMethod(const std::string &taskId, const Request &request) {
34 PRINT_DEBUG("method2 taskId: %s, request: %s\n", taskId.c_str(), request.toString().c_str());
35
36 // 异步处理
37 std::thread t(workMethod2, taskId);
38 t.detach();
39
40 return Response::success();
41 }
42
43 REGISTER_SERVICE_ASYNC_HANDLER("asyncMethod", handleAsyncMethod)