qicosmos / cinatra

modern c++(c++20), cross-platform, header-only, easy to use http framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

coro_http_client 不支持多线程调用?

ganyao114 opened this issue · comments

上一个请求还未完成的时候再次请求会会返回 404

client 不支持多线程,client 是基于协程的,你用多线程不如用多协程。

但问题是 coro_http_client 没办法复用,使用场景中请求的携程会被分配到不同的工作线程中执行,example 中的 case 是临时的 coro_http_client + 顺序 co_await 所以没什么问题

没明白你的场景,可以写个伪代码来看看要怎么用。

为了性能更好,最好不用使用同步接口,你应该用co_await client.async_post/get 这类的接口,这类接口是异步的,不会阻塞调用线程。
如果你希望同时发起多个异步请求,可以使用collectall,参考这个代码:
https://github.com/qicosmos/cinatra/blob/master/press_tool/main.cpp#L148

client不是线程安全的。
有两个选择:
1.不复用client。每个线程自己持有一个client。请求会在一个全局线程池中处理。
2.如果想复用socket连接,此时你需要一个channel。
你可以使用yalantinglibs里面的coro_io::channel<coro_http::coro_http_client>。

channel是线程安全的,而client不是线程安全的。

Thx