版本12和13间的区别
于2010-01-04 23:01:22修订的的版本12
大小: 7931
编辑: 116
备注:
于2010-01-05 01:56:01修订的的版本13
大小: 8351
编辑: 116
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 112: 行号 112:
通道对象是用来在小任务之间进行通讯用的。

在一个小任务里往一个通道发送东西,另一个在这个通道等待接收东西的小任务就会被恢复。如果没有小任务在接收,发送方会被挂起。

在一个小任务里接收一个通道的东西,另一个在这个通道上等待发送的小任务就会被恢复。如果没有发送者,接收方会被挂起。

英文原版: http://www.disinterest.org/resource/stackless/2.6.4-docs-html/stackless-python.html

Stackless Python

Stackless Python 是Python语言的一个增强版本。它让程序员可以获得基于线程的程序的优点,同时又避免传统线程带来的性能和复杂度问题。Stackless Python为Python语言添加的微线程(microthread)是一种方便、廉价、轻量级的工具,如果使用得当,它不仅可以提供一种构建应用程序或者框架的方法,而且能改进程序的结构和可读性。

如果你在你安装的Python附带的文档中读到这篇文章,这说明你安装的已经是Stackless Python而非标准的Python。

1. 概述

除了Stackless Python新增的功能部分,Stackless Python的其他部分的行为和标准的Python完全一样,用法也完全一样。Stackless的新增的功能,是通过stackless模块暴露出来的框架来使用的。

2. 你需要知道的

Stackless Python只提供了一个最基本的框架,它没有附带任何支撑功能,只是满足构建一个特定用途的框架时可能出现的一般需求。

2.1. 阻塞的操作

如果调用的操作会阻塞Python解释器,用户需要注意,这个操作也会阻塞所有运行中的小任务。Python解释器会一直阻塞,调度器也会阻塞在执行操作的小任务上,直到那个小任务结束。阻塞解释器的操作常常是和同步IO(文件读写、套接字操作、进程间通讯等)有关的,也要注意time.sleep()。建议用户使用异步版本的IO函数。

某些第三方模块可以用Stackless兼容的方式来代替一些标准库中模块。这种方法的好处是,原来使用标准模块的其他模块也可以在替代的模块上工作。Stackless socket模块是最常使用的替代模块。

2.2. 异常

小任务里面出现的某些异常,是期望能沿着调用栈向上直到调度器的。这意味着简单的使用except语句可能会导致难以查出的问题。

有关这个问题的描述可以参看TaskletExit异常。

2.3. 调试

Stackless的调度机制改变了Python调试挂钩的工作方式。调试挂钩需要以小任务为单位设置,而不是线程。但是很少有调试器(标准库中一个也没有)考虑了这一点。这导致如果不进行一些特殊处理将不能进行调试。

这个问题的详细描述,参看Stackless调试文档。

3. 外部资源

除了此文档外,还有一系列资源:

  • Stackless邮件列表
  • Stackless样例
  • Grant Olson的入门教程:Introduction to Concurrent Programming with Stackless Python.

4. 历史

Continuations are a feature that require the programming language they are part of, to be implemented in a way conducive to their presence. In order to add them to the Python programming language, Christian Tismer modified it extensively. The primary way in which it was modified, was to make it Stackless. And so, his fork of Python was named Stackless Python.

Now, storing data on the stack locks execution on an operating system thread to the current executing functionality, until that functionality completes and the stack usage is released piece by piece. In order to add continuations to Python, that data needed to be stored on the heap instead, therefore decoupling the executing functionality from the stack. With the extensive changes this required in place, Christian released Stackless Python.

Maintaining the fork of a programming language is a lot of work, and when the programming language changes in ways that are incompatible with the changes in the fork, then that work is sigificantly increased. Over time it became obvious that the amount of changes to Python were too much weight to carry, and Christian contemplated a rewrite of Stackless. It became obvious that a simpler approach could be taken, where Stackless was no longer stackless and no longer had continuations.

Following the rewrite, a framework was designed and added inspired by coming from CSP and the Limbo programming language. From this point on, Stackless was in a state where it contained the minimum functionality to give the benefits it aimed to provide, with the minimum amount of work required to keep it maintained.

A few years later in 2004, while sprinting on Stackless in Berlin, Christian and Armin Rigo came up with a way to take the core functionality of Stackless and build an extension module that provided it. This was the creation of greenlets, which are very likely a more popular tool than Stackless itself today. The greenlet source code in practice can be used as the base for green threading functionality not just in Python, but in other programming languages and projects.

With Stackless Python a solid product, Christian’s focus moved onto other projects, PyPy among them. One of his interests in PyPy was a proper implementation of the Stackless functionality, where it could be integrated as a natural part of any Python built.

For a while, Stackless Python languished, with no new versions to match the releases of Python itself. Then in 2006, CCP sent Kristjan Valur Jonsson and Richard Tew to PyCon where they sprinted with the aid of Christian Tismer. The result was an up to date release of Stackless Python. From this point in time, maintaining and releasing Stackless Python has been undertaken by Richard and Kristjan.

stackless — 内置的扩展模块

使用stackless模块是程序员使用Stackless Python的增强功能的方法。

1. 函数

2. 属性

3. 异常

小任务(Tasklet) — 轻量级的线程

小任务将函数包装起来,允许函数以微线程来加载并在调度器中执行

加载一个小任务:

stackless.tasklet(callable)(*args, **kwargs)

这是最常见的加载小任务的方法。这不仅创建了一个小任务,而且自动将它插入到调试器中。

例子 - 加载一个更具体的小任务:

>>> def func(*args, **kwargs):
...     print "scheduled with", args, "and", kwargs
...
>>> stackless.tasklet(func)(1, 2, 3, string="test")
<stackless.tasklet object at 0x01C58030>
>>> stackless.run()
scheduled with (1, 2, 3) and {'string': 'test'}

1. 小任务、main、current等

有两种需要特别注意的小任务,main小任务和current小任务。

main小任务是固定的,是你的应用程序的开始执行的地方。而成为main线程的机遇是它运行了调度器。

current小任务是当前正在运行的小任务。如果没有其他小任务在执行,它可能是main小任务。否则,它就是调度器里面可以被执行的小任务的链表的第一个,那就是正在执行的。

例子 - main小任务是current小任务吗:

stackless.main == stackless.current

例子 - 当前小任务是main小任务吗:

stackless.current.is_main == 1

例子 - 有多少小任务在被调度:

stackless.runcount

注意:

The main tasklet factors into the stackless.runcount value. If you are checking how many tasklets are in the scheduler from your main loop, you need to keep in mind that there will be another tasklet in there over and above the ones you explicitly created.

2. tasklet类

通道(Channel) — 小任务之间的通讯

通道对象是用来在小任务之间进行通讯用的。

在一个小任务里往一个通道发送东西,另一个在这个通道等待接收东西的小任务就会被恢复。如果没有小任务在接收,发送方会被挂起。

在一个小任务里接收一个通道的东西,另一个在这个通道上等待发送的小任务就会被恢复。如果没有发送者,接收方会被挂起。

1. 通道与线程

2. channel类

调度器 — 小任务如何运行

1. 合作式调度(Cooperative scheduling)

2. 抢占式调度(Pre-emptive scheduling)

3. 异常

调试与跟踪 — Stackless有什么不同

1. settrace与小任务

线程 — 线程与Stackless

1. 每线程一个调度器

2. 通道是线程安全的

Pickling — 运行中的小任务的序列化

StacklessPython (2010-01-24 11:31:07由125编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.