无堆栈的python和multicores?

2024-04-25 21:37:46 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我在玩弄Stackless Python,一个问题突然出现在我的脑海中,也许这是“假设”或“普通”的知识,但我在stackless site上找不到它的真正写法。

Stackless Python是否利用多核cpu?在普通的Python中,GIL一直存在,要使用多个核心,您需要使用多个进程,这对Stackless也是正确的吗?


Tags: 利用核心进程sitecpugilstackless
1条回答
网友
1楼 · 发布于 2024-04-25 21:37:46

Stackless python没有利用它运行的任何类型的多核环境 这是关于Stackless的常见误解,因为它允许程序员利用基于线程的编程。对许多人来说,这两者紧密地交织在一起,但实际上是两个独立的东西。

在内部Stackless使用循环调度程序来调度每个tasklet(微线程),但是没有一个tasklet可以与另一个tasklet并发运行。这意味着,如果一个微线程正忙,其他微线程必须等到该微线程放弃控制。默认情况下,调度程序不会停止一个tasklet并给另一个tasklet分配处理器时间。tasklet的责任是使用Stackless.schedule()将自身调度回调度队列的末尾,或者完成其计算。

因此,所有微线程都是按顺序执行的,即使有多个核心可用。

Stackless没有多核支持的原因是这使得线程变得更加容易。这正是stackless的意义所在:

from the official stackless website

Stackless Python is an enhanced version of the Python programming language. It allows programmers to reap the benefits of thread-based programming without the performance and complexity problems associated with conventional threads. The microthreads that Stackless adds to Python are a cheap and lightweight convenience which can if used properly, give the following benefits:

  • Improved program structure.
  • More readable code.
  • Increased programmer productivity.

这里有一个link来获得更多关于多核和无堆栈的信息。

相关问题 更多 >