Scala Cats Effects - IO Async Shift - How Does it Work?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 12:42:35

For example, why is it that after I call "forkAsync", if I don't call "IO.shift(Global)", the subsequent synchronous IO objects are run in "pool-1-thread-1".

The more important question is why would you expect it to evaluate the "subsequent synchronous IO objects" on global?

IO doesn't have internally the notion of thread-pools, it doesn't know about global, so it cannot shift back to your default thread-pool, therefore you need to trigger a manual shift indeed.

Upgrade to the latest version 1.0.0 and you also have evalOn in ContextShift which will execute an IO action on a specified thread-pool and then shift back to your "global", which I suppose is what you want.

Also, what is the difference between forkAsync and forkSync in this example?

Your forkSync triggers the execution of the Runnable, but does not wait for its completion. It's a fire and forget. Which means that subsequent chained actions will not do back-pressuring.

Some advice:

  1. upgrade to the latest version (1.0.0)
  2. read the docs at: https://typelevel.org/cats-effect/datatypes/io.html
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!