qicosmos / rest_rpc

modern C++(C++11), simple, easy to use rpc framework
MIT License
1.7k stars 370 forks source link

怎么配合cinatra使用异步接口? #72

Closed Kidsunbo closed 2 years ago

Kidsunbo commented 2 years ago

最近在使用cinatra和rest_rpc,我目前以cinatra为主,如果我理解的没问题的话,cinatra的回调里调用rest_rpc同步接口,会阻塞一个线程,性能上有损耗,如果使用异步接口,cinatra的回调会快速返回并发送response,也存在问题。 而且两个库各自有一套io_service的话,目前我的做法是交替执行poll_one,这个是推荐的做法吗?

qicosmos commented 2 years ago

没必要交替执行poll_one, 用rest_rpc异步接口。 cinatra支持在另外的线程中response哈,例子:https://github.com/qicosmos/cinatra/blob/master/example/main.cpp#L276

qicosmos commented 2 years ago

另外如果你调用rest_rpc耗时不是很大,比如不超过几ms的话,把server的线程数设置大一些比如核数的2-3倍,然后调用同步接口也是没问题的,性能也不会受多大影响,你可以做一个benchmark。

Kidsunbo commented 2 years ago

另外如果你调用rest_rpc耗时不是很大,比如不超过几ms的话,把server的线程数设置大一些比如核数的2-3倍,然后调用同步接口也是没问题的,性能也不会受多大影响,你可以做一个benchmark。

哦哦,目前我还是用的同步的,因为想看看能不能切换成异步的接口。之前没发现cinatra可以支持另外的线程中response,所以想不到怎么在cinatra里面用回调。现在看起来调用一下set_delay就可以实现了,挺好用的。感谢感谢。