Python/Erlang:Twisted、Stackless、Greenlet、Eventlet、协程之间有什么区别?它们与 Erlang 进程相似吗?

发布于 2024-10-04 20:12:20 字数 184 浏览 6 评论 0原文

我不完全的理解是,Twisted、Stackless、Greenlet、Eventlet、Coroutines 都利用了非常轻量级且快速切换的异步网络 IO 和用户态线程。但我不确定它们之间有什么区别。

而且它们听起来与 Erlang 进程非常相似。它们几乎是同一件事吗?

任何可以帮助我更多地理解这个主题的人将不胜感激。

My incomplete understanding is that Twisted, Stackless, Greenlet, Eventlet, Coroutines all make use of async network IO and userland threads that are very lightweight and quick to switch. But I'm not sure what are the differences between them.

Also they sound very similar to Erlang processes. Are they pretty much the same thing?

Anyone who could help me understand this topic more would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你手 2024-10-11 20:12:20

首先,非阻塞 I/O 与绿色线程或协程没有任何共同之处,但它会影响它们的调度方式。

现在:

  • Twisted 是一个经典的非阻塞 I/O 框架 - 应用程序代码是使用回调以异步风格编写的。
  • Geventeventlet 使用 coroutines/greenthreads/greenlet 的 greenlet 库。有一个专用的 greenlet 用于运行 eventloop(对于 gevent,它是 C 编码的 libevent 的事件循环)。当任意 greenlet 开始等待某些 I/O 操作处理时,它只是将执行交给事件循环,该事件循环启动另一个 greenlet 来执行(准备好执行某些 I/O)。这称为协作多任务处理——每个 greenlet 自行决定何时将控制权返回给其他 greenlet。
  • Stackless 有tasklet,与greenlet类似,但也可以使用抢占式模型进行调度——这意味着调度程序可以随时停止该tasklet的执行并开始执行另一个tasklet(这就是操作系统线程和Erlang进程的工作方式)。另外,Stackless 不提供任何开箱即用的非阻塞 I/O 设施,因此如果您通过 stdlib 执行 I/O — 它将阻塞整个操作系统线程,因此在您等待 I/O 时,没有其他 tasklet 可以执行/O。有人尝试为 Stackless 提供 gevent 库的端口,但我不知道进展如何。

First of all, non-blocking I/O has nothing in common with green threads or coroutines, but it can affect how they're scheduled.

Now:

  • Twisted is a classic non-blocking I/O framework — application code is written in async style using callbacks.
  • Gevent and eventlet use the greenlet library for coroutines/greenthreads/greenlets. There is one dedicated greenlet for running the eventloop (in case of gevent it's C-coded libevent's event loop). When arbitrary greenlet begins to wait for some I/O operation to process, it just gives execution to the event loop, which starts another greenlet for execution (which is ready to do some I/O). This is called cooperative multitasking — each greenlet decides itself when to return control to other greenlets.
  • Stackless has tasklets, which are similar to greenlets, but can also be scheduled with a preemptive model — that means the scheduler can stop the tasklet execution at any time and start execution of another tasklet (which is how OS threads and Erlang processes work). Also, Stackless does not provide any non-blocking I/O facilities out of the box, so if you do I/O via stdlib — it will block the entire OS thread, so no other tasklet can execute while you're waiting on I/O. There have been attempts to provide a port of the gevent library for Stackless but I don't know how it's going.
流年已逝 2024-10-11 20:12:20

比较 Stackless 时你几乎是对的
到格林莱特。缺少的是:

Stackless 本身并没有添加某些东西。相反,在 Stackless 5 年后发明的 Greenlet 删除了某些东西。它编写得足够简单,可以构建为扩展模块而不是替代解释器。

这真的很有趣——Stackless 有更多的功能,切换效率大约是原来的 10 倍,并且提供执行状态的 pickling。

Greenlet 仍然获胜,可能只是因为作为扩展模块易于使用。因此,我正在考虑通过酸洗扩展 Greenlet 来恢复该过程。也许这会再次改变情况:-)

You are almost right when comparing Stackless
to Greenlet. The missing thing is:

Stackless per se does not add something. Instead, Greenlet, invented 5 years after Stackless, removes certain things. It is written simple enough to be built as an extension module instead of a replacement interpreter.

This is really funny—Stackless has many more features, is about 10 times more efficient on switching, and provides pickling of execution state.

Greenlet still wins, probably only due to ease of use as an extension module. So I'm thinking about reverting the process by extending Greenlet with pickling. Maybe that would change the picture, again :-)

撧情箌佬 2024-10-11 20:12:20

诱饵上钩了! (欢迎修复!):

严重:

  • 扭曲:单线程。通过使用“回调”和“延迟”惯用法实现非阻塞行为。与 Node.js 类似。
  • greenlet / eventlet :使用“绿色线程”(内存部分?)来实现非阻塞 io。实际上用它们的版本修补了标准 CPython IO,因此代码仍然以阻塞/顺序的方式编写。
  • stackless:http://www.stackless.com/。没有使用过它,看起来它添加了“微线程”和其他细节? stackless 示例惯用法
  • 协程:SO 上的协程

这些都不像 Erlang 进程那样轻量或得到良好支持。

Bait taken! (fixes welcome!):

Grossly:

  • twisted: single threaded. achieves non-blocking behaviour via use of 'callbacks' and 'deferred' idioms. Similar to node.js.
  • greenlet / eventlet : use 'green threads' (sections of memory?) to achieve non-blocking io. Actually patches the standard CPython IO with their versions, so code is still written as though it is blocking / sequential.
  • stackless: http://www.stackless.com/. Haven't used it, looks like it adds 'microthreads' and other niceties? stackless example idioms
  • coroutines: coroutines on SO

None of these are as light or well-supported as Erlang processes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文