greenlet: Lightweight concurrent programming

简介: https://greenlet.readthedocs.io/en/latest/MotivationThe “greenlet” package is a spin-off of Stackless, a version of CPython that supports micro-threads called “tasklets”.

https://greenlet.readthedocs.io/en/latest/

Motivation

The “greenlet” package is a spin-off of Stackless, a version of CPython that supports micro-threads called “tasklets”. Tasklets run pseudo-concurrently (typically in a single or a few OS-level threads) and are synchronized with data exchanges on “channels”.

A “greenlet”, on the other hand, is a still more primitive notion of micro-thread with no implicit scheduling; coroutines, in other words. This is useful when you want to control exactly when your code runs. You can build custom scheduled micro-threads on top of greenlet; however, it seems that greenlets are useful on their own as a way to make advanced control flow structures. For example, we can recreate generators; the difference with Python’s own generators is that our generators can call nested functions and the nested functions can yield values too. (Additionally, you don’t need a “yield” keyword. See the example in test/test_generator.py
).

Greenlets are provided as a C extension module for the regular unmodified interpreter.

Example

Let’s consider a system controlled by a terminal-like console, where the user types commands. Assume that the input comes character by character. In such a system, there will typically be a loop like the following one:

def process_commands(*args):
    while True:
        line = ''
        while not line.endswith('\n'):
            line += read_next_char()
        if line == 'quit\n':
            print "are you sure?"
            if read_next_char() != 'y':
                continue    # ignore the command
        process_command(line)

Now assume that you want to plug this program into a GUI. Most GUI toolkits are event-based. They will invoke a call-back for each character the user presses. [Replace “GUI” with “XML expat parser” if that rings more bells to you :-)] In this setting, it is difficult to implement the read_next_char() function needed by the code above. We have two incompatible functions:

def event_keydown(key):
    ??

def read_next_char():
    ?? should wait for the next event_keydown() call

You might consider doing that with threads. Greenlets are an alternate solution that don’t have the related locking and shutdown problems. You start the process_commands() function in its own, separate greenlet, and then you exchange the keypresses with it as follows:

def event_keydown(key):
         # jump into g_processor, sending it the key
    g_processor.switch(key)

def read_next_char():
        # g_self is g_processor in this simple example
    g_self = greenlet.getcurrent()
        # jump to the parent (main) greenlet, waiting for the next key
    next_char = g_self.parent.switch()
    return next_char

g_processor = greenlet(process_commands)
g_processor.switch(*args)   # input arguments to process_commands()

gui.mainloop()

In this example, the execution flow is: when read_next_char() is called, it is part of the g_processor greenlet, so when it switches to its parent greenlet, it resumes execution in the top-level main loop (the GUI). When the GUI calls event_keydown(), it switches to g_processor, which means that the execution jumps back wherever it was suspended in that greenlet – in this case, to the switch() instruction in read_next_char() – and the key argument in event_keydown() is passed as the return value of the switch() in read_next_char().

Note that read_next_char() will be suspended and resumed with its call stack preserved, so that it will itself return to different positions in process_commands() depending on where it was originally called from. This allows the logic of the program to be kept in a nice control-flow way; we don’t have to completely rewrite process_commands() to turn it into a state machine.

Usage

Introduction

A “greenlet” is a small independent pseudo-thread. Think about it as a small stack of frames; the outermost (bottom) frame is the initial function you called, and the innermost frame is the one in which the greenlet is currently paused. You work with greenlets by creating a number of such stacks and jumping execution between them. Jumps are never implicit: a greenlet must choose to jump to another greenlet, which will cause the former to suspend and the latter to resume where it was suspended. Jumping between greenlets is called “switching”.

When you create a greenlet, it gets an initially empty stack; when you first switch to it, it starts to run a specified function, which may call other functions, switch out of the greenlet, etc. When eventually the outermost function finishes its execution, the greenlet’s stack becomes empty again and the greenlet is “dead”. Greenlets can also die of an uncaught exception.

For example:

from greenlet import greenlet

def test1():
    print 12
    gr2.switch()
    print 34

def test2():
    print 56
    gr1.switch()
    print 78

gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()

The last line jumps to test1, which prints 12, jumps to test2, prints 56, jumps back into test1, prints 34; and then test1 finishes and gr1 dies. At this point, the execution comes back to the original gr1.switch() call. Note that 78 is never printed.

Parents

Let’s see where execution goes when a greenlet dies. Every greenlet has a “parent” greenlet. The parent greenlet is initially the one in which the greenlet was created (this can be changed at any time). The parent is where execution continues when a greenlet dies. This way, greenlets are organized in a tree. Top-level code that doesn’t run in a user-created greenlet runs in the implicit “main” greenlet, which is the root of the tree.

In the above example, both gr1 and gr2 have the main greenlet as a parent. Whenever one of them dies, the execution comes back to “main”.

Uncaught exceptions are propagated into the parent, too. For example, if the above test2() contained a typo, it would generate a NameError that would kill gr2, and the exception would go back directly into “main”. The traceback would show test2, but not test1. Remember, switches are not calls, but transfer of execution between parallel “stack containers”, and the “parent” defines which stack logically comes “below” the current one.

Instantiation

greenlet.greenlet is the greenlet type, which supports the following operations:

greenlet(run=None, parent=None)
Create a new greenlet object (without running it). run is the callable to invoke, and parent is the parent greenlet, which defaults to the current greenlet.
greenlet.getcurrent()
Returns the current greenlet (i.e. the one which called this function).
greenlet.GreenletExit
This special exception does not propagate to the parent greenlet; it can be used to kill a single greenlet.
The greenlet type can be subclassed, too. A greenlet runs by calling its run attribute, which is normally set when the greenlet is created; but for subclasses it also makes sense to define a run method instead of giving a run argument to the constructor.

Switching

Switches between greenlets occur when the method switch() of a greenlet is called, in which case execution jumps to the greenlet whose switch() is called, or when a greenlet dies, in which case execution jumps to the parent greenlet. During a switch, an object or an exception is “sent” to the target greenlet; this can be used as a convenient way to pass information between greenlets. For example:

def test1(x, y):
    z = gr2.switch(x+y)
    print z

def test2(u):
    print u
    gr1.switch(42)

gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch("hello", " world")
目录
相关文章
|
9月前
|
Java
Exploring Java's AbstractQueuedSynchronizer (AQS)
AbstractQueuedSynchronizer (AQS) is a key building block in Java's concurrent programming paradigm. With its powerful capabilities, it simplifies the development of custom synchronizers and facilitates efficient thread synchronization and control. Understanding AQS is essential for any Java develope
35 1
|
9月前
|
Java
Understanding ConcurrentHashMap in Java
ConcurrentHashMap is a robust and efficient data structure that facilitates concurrent access to a hash map while ensuring thread safety. Its unique segmentation approach and fine-grained locking make it a top choice for concurrent programming in Java. By using ConcurrentHashMap correctly and unders
42 0
|
10月前
|
设计模式 算法 安全
Java Concurrencyin Practice 并发编程实践系列1
Java Concurrencyin Practice 并发编程实践系列1
|
缓存 安全 Java
Java 内存模型中的 happen-before 是什么?
Java 内存模型中的 happen-before 是什么?
90 0
Java 内存模型中的 happen-before 是什么?
|
存储 缓存 安全
看完这本《Effective Java》,我悟了 —— 日更ing
类与接口篇 15 使类和成员的可访问性最小化 16 在public类中使用访问方法而不是public属性 17 可变性最小化原则 18 复合优先于继承 19 要么设计继承并提供文档,要么禁止继承 20 接口优于抽象类 21 为后代设计接口 22 接口只用于定义类型 23 类层次优于标签类 24 静态成员类优于非静态成员类 (非)静态成员类 匿名类、局部类 25 一个文件只定义一个顶级类
看完这本《Effective Java》,我悟了 —— 日更ing
|
存储 算法 Java
Kotlin for Java Developers 学习笔记
Kotlin for Java Developers 学习笔记
2591 0
|
JavaScript Java Go
Rise of Kotlin: The Programming Language for the Next Generation
Rise of Kotlin: The Programming Language for the Next Generation https://hackernoon.
1518 0
|
Kotlin
My favorite examples of functional programming in Kotlin
My favorite examples of functional programming in Kotlin One of the great things about Kotlin is that it supports functional programming.
940 0
|
缓存 安全 Java
《Effective Java 》系列一
image.png 目录 第二章 创建和销毁对象 1 考虑用静态工厂方法替代构造器 2 遇到多个构造器参数时要考虑用构件器 3 用私有构造器或者枚举类型强化Singleton属性 4 通过私有构造器强化不可实例化的能力 5 避免创建不必要的...
923 0
Understanding Generics and Variance in Kotlin
https://proandroiddev.com/understanding-generics-and-variance-in-kotlin-714c14564c47
864 0