TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Coroutines and Fibers: Why and When

57 pointsby stelabourasover 9 years ago

7 comments

vvandersover 9 years ago
Coroutines are also fantastic for scripting Game AI:<p><pre><code> local player = nil while(player == nil) do yeild(0) player = LookForPlayer(...) end while(WalkTowardsPlayer(player)) do yeild(0) end while(!PlayerDead(player)) do MeleePlayer(player) yield(0) end </code></pre> Makes state management super-simple and is something you can easily teach to designers as well. This is partly why you see Lua show up in so many game engines.
评论 #10912783 未加载
vbitover 9 years ago
I generally prefer coroutines myself but this article doesn&#x27;t cover some points well.<p>&#x27;Fair scheduling&#x27; - nobody writes single threaded programs that process requests sequentially. It&#x27;s either a thread-per-request system vs. a coroutine-per-request system. Threads are generally going to be more fair at scheduling since they are preemptive. Most coroutines are cooperative (Python, Lua) - so a long running coroutine (stuck doing some CPU cycle) will block all other inflight requests and cause latency variance. Some systems are preemptive (Erlang) so they don&#x27;t suffer from the variance.<p>The benefit of coroutines is you can have a very large number of coroutines, probably orders of magnitude more than the number of threads - so coroutine-per-request models will scale much more than threads-per-request. The article is spot on about the context switching cost - it&#x27;s so much cheaper to switch between coroutines.<p>You can also use multiple request per threads and use async IO, but that&#x27;s the same as using coroutines with a much worse programming model.
评论 #10913889 未加载
nqzeroover 9 years ago
i&#x27;m using java fibers (<a href="https:&#x2F;&#x2F;github.com&#x2F;kilim&#x2F;kilim" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kilim&#x2F;kilim</a>) for a database<p>for anyone interested in using fibers inside a java webapp, i just wrote up a quick survey of async java servlet performance: <a href="http:&#x2F;&#x2F;blog.nqzero.com&#x2F;2016&#x2F;01&#x2F;asynchronous-java-webserver-hello-world.html" rel="nofollow">http:&#x2F;&#x2F;blog.nqzero.com&#x2F;2016&#x2F;01&#x2F;asynchronous-java-webserver-h...</a><p>(based TechEmpower plaintext benchmark, but limited to java async)<p>the model is that you use async in the server, and then bridge to a fiber implementation and then complete the async response when the fiber completes. for handling high latency low cpu processing on the server, this technique allows handling a huge number of connections
评论 #10913091 未加载
avdiciusover 9 years ago
Coroutines seem to be a very popular topic. And again my shameless plug, I did exactly the thing described in this article in my project: <a href="https:&#x2F;&#x2F;github.com&#x2F;ademakov&#x2F;MainMemory" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ademakov&#x2F;MainMemory</a>
dblohm7over 9 years ago
One caveat with coroutines and fibers is that all the code that runs on them must be fully aware that they are running on said coroutines and fibers.
评论 #10913597 未加载
评论 #10912585 未加载
评论 #10912433 未加载
TheGrassyKnollover 9 years ago
For Python: (might help someone) <a href="http:&#x2F;&#x2F;www.dabeaz.com&#x2F;coroutines&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.dabeaz.com&#x2F;coroutines&#x2F;</a>
iambvkover 9 years ago
I don&#x27;t understand how resumable functions can be used to build stack-full coroutines, like in Go language, can anyone give an example or some links?
评论 #10911793 未加载