愚蠢的 IOThreadPoolExecutor 与 CPUThreadPoolExecutor

问题描述

我正在尝试更多地了解我正在处理的这个代码库所使用的异步抽象。

我正在阅读库中两个异步执行器池的 RowDataPacket { id: '573991320897716224',bank: '0',wallet: '0',unlimited: '0' } 文档,Folly 用于 IOThreadPoolExecutor 绑定任务,io 用于 cpuThreadPoolExecutor绑定任务 (https://github.com/facebook/folly/blob/master/folly/docs/Executors.md)。

我正在阅读说明,但我不明白主要区别。 cpu 似乎是围绕 IOThreadPoolExecutorevent_fd 循环构建的,而 epoll 使用队列和信号量。

但这并没有告诉我太多的好处和权衡。

解决方法

在高级别 IPThreadPoolExecutors 应仅在您需要 EventBase 池时使用。如果您需要一组工作线程,请使用 CPUThreadPoolExecutor。

CPUThreadPoolExecutor

包含一系列优先级队列,这些队列不断被一系列工作人员接收。每个工作线程在创建后执行 threadRun()。 ThreadRun() 本质上是一个无限循环,它从任务队列中拉出一个任务并执行它。如果任务在获取时已经过期,则执行 expire 回调而不是任务本身。

cputhreadpoolexecutor

IOThreadPoolExecutor

每个 IO 线程运行自己的 EventBase。 IOThreadPoolExecutor 不是像 CPUThreadPoolExecutor 那样从任务队列中拉取任务,而是将事件注册到下一个 IO 线程的 EventBase。然后每个 IO 线程为其 EventBase 调用 loopForEver(),它本质上调用 epoll() 来执行异步 io。

enter image description here

因此,大多数时候您可能应该使用 CPUThreadPoolExecutor,因为这是拥有一组工作线程的常见用例。