sdbusplus:通过协程yield_method_call异步调用service的method
2023-04-18 16:46:54 时间
sdbusplus还提供了通过协程yield_method_call异步调用的方式:
//async_coroutine.cpp
#include <iostream>
#include <boost/asio.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
using namespace std;
using namespace sdbusplus;
void asyncCallCoroutines(shared_ptr<sdbusplus::asio::connection> conn, boost::asio::yield_context yield)
{
boost::system::error_code ec;
cout<<"1 yield_method_call AddInt begin"<<endl;
int res1 = conn->yield_method_call<int>(yield, ec,
"calculate.service", "/calculate_obj", "calculate_infterface.data", "AddInt", 1, 2);
if (ec)
{
cout<<"yield_method_call failed"<<endl;
}
else
{
cout<<"yield_method_call res="<<res1<<endl;
}
cout<<"2 yield_method_call AddInt end"<<endl;
ec.clear();
cout<<"3 yield_method_call AddInt begin"<<endl;
int res2 = conn->yield_method_call<int>(yield, ec,
"calculate.service", "/calculate_obj", "calculate_infterface.data", "AddInt", 3, 4);
if (ec)
{
cout<<"yield_method_call failed"<<endl;
}
else
{
cout<<"yield_method_call res="<<res2<<endl;
}
cout<<"4 yield_method_call AddInt end"<<endl;
}
int main()
{
boost::asio::io_context io;
auto conn = make_shared<sdbusplus::asio::connection>(io);
cout<<"asyncCallCoroutines begin"<<endl;
boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
asyncCallCoroutines(conn, yield);
});
cout<<"asyncCallCoroutines end"<<endl;
io.run();
return 0;
}
编译程序,需要加入协程库:
g++ -o async_coroutine ./async_coroutine.cpp -lsdbusplus -lsystemd -lboost_coroutine
运行程序输出:
asyncCallCoroutines begin
asyncCallCoroutines end
1 yield_method_call AddInt begin
yield_method_call res=3
2 yield_method_call AddInt end
3 yield_method_call AddInt begin
yield_method_call res=7
4 yield_method_call AddInt end
通过输出:
asyncCallCoroutines begin
asyncCallCoroutines end
可以看出协程本身是被异步调用的
但是协程的内部是顺序调用的,有点类似是同步调用的:
1 yield_method_call AddInt begin
yield_method_call res=3
2 yield_method_call AddInt end
3 yield_method_call AddInt begin
yield_method_call res=7
4 yield_method_call AddInt end
相关文章
- AAAI 2023 | 一种通用的粗-细视觉Transformer加速方案
- 服务拆分和远程调用(微服务)
- Windows中Powershell中的 rm -rf 等效命令
- printf 理解
- Hudi Java Client总结|读取Hive写Hudi代码示例
- 参考文档
- 单词2
- Redisson的看门狗机制
- fileversion 55.0 this version of the Java Runtime only recognizes class file versions up to 52.0:已解决
- linux系统 u盘无法复制粘贴显示Read-only file system
- 随机输入的两个数之间的偶数和
- C#调用基于UVC协议的摄像头扩展接口
- try-catch的使用以及细节
- Mybatis-Plus--LambdaQueryChainWrapper--使用/实例
- RabbitMQ发布与订阅模式类型
- Katalon-指定浏览器窗口大小(Set View Port Size)
- Crossformer: Transformer Utilizing Cross-Dimension Dependency for Multivariate Time Series Forecasting
- 校园招聘管理系统的设计与实现毕业设计源码161815
- Find the smallest integer in the array
- 如何将项目部署到服务器上(全套教程)