Boost asio http 客户端 - io_service.run()
我正在尝试弄清楚 io_service.run() 函数如何在客户端中运行。这与这个问题有些相关,但在另一端连接的。在客户端情况下,没有接受处理程序,那么 io_service.run() 什么时候返回?
理想情况下,我想运行一次(据我了解,每个线程,欢迎更正,每个调用 io_service.run() 的线程基本上都会添加到 io_service 线程池中),然后发送大量请求,而无需需要再次调用reset
、run
。当 io_service 没有工作可做时,发送的请求中将会出现间隙,但这些间隙的出现在很大程度上是不确定的,具体取决于用户行为。
I'm trying to work out how the io_service.run() function operates in a client. This is somewhat related to this question but on the other end of the connection. In a client situation there is no accept handler so when will the io_service.run()
return?
Ideally I want to run it once (per thread, as I understand it, correction welcome, each thread that calls io_service.run()
is basically added to the io_service thread pool) then send lots of requests without needing to call reset
, run
again. There will be gaps in the requests being sent when the io_service will have no work to do but the occurrence of those gaps is largely non-deterministic, based on user behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,
io_service
当它耗尽工作时返回,例如在服务器中,accept 调用为其提供工作。为了防止 io_service 耗尽工作,请查看io_service::work
类。此链接 取自此 SO 帖子,提供了以下概述如何使用工作对象。特别是,将工作对象存储在
shared_ptr
中并在想要停止应用程序等时重置shared_ptr
是一个有用的习惯用法。通过不调用io_service::stop()
方法(该方法也可用于停止io_service
),您可以干净地完成所有必需的工作嗯>。As you stated, an
io_service
returns when it runs out of work e.g. in a server the accept call provides it with work. To prevent an io_service from running out of work, have a look at theio_service::work
class.This link taken from this SO post, provides an overview of how the work object can be used. In particular storing the work object in a
shared_ptr
and resetting theshared_ptr
when wanting to stop the application, etc. is a useful idiom. By not calling theio_service::stop()
method (which can also be used to stop theio_service
) you allow all required work to be completed cleanly.