中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

python greenlet
來源:易賢網 閱讀:1924 次 日期:2015-04-27 09:44:14
溫馨提示:易賢網小編為您整理了“python greenlet”,方便廣大網友查閱!

greenlet: Lightweight concurrent programming

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(' '):

line += read_next_char()

if line == 'quit ':

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 the 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”)

This prints “hello world” and 42, with the same order of execution as the previous example. Note that the arguments of test1() and test2() are not provided when the greenlet is created, but only the first time someone switches to it.

Here are the precise rules for sending objects around:

g.switch(args, *kwargs)

Switches execution to the greenlet g, sending it the given arguments. As a special case, if g did not start yet, then it will start to run now.

Dying greenlet

If a greenlet’s run() finishes, its return value is the object sent to its parent. If run() terminates with an exception, the exception is propagated to its parent (unless it is a greenlet.GreenletExit exception, in which case the exception object is caught and returned to the parent).

Apart from the cases described above, the target greenlet normally receives the object as the return value of the call to switch() in which it was previously suspended. Indeed, although a call to switch() does not return immediately, it will still return at some point in the future, when some other greenlet switches back. When this occurs, then execution resumes just after the switch() where it was suspended, and the switch() itself appears to return the object that was just sent. This means that x = g.switch(y) will send the object y to g, and will later put the (unrelated) object that some (unrelated) greenlet passes back to us into x.

Note that any attempt to switch to a dead greenlet actually goes to the dead greenlet’s parent, or its parent’s parent, and so on. (The final parent is the “main” greenlet, which is never dead.)

Methods and attributes of greenlets

g.switch(args, *kwargs)

Switches execution to the greenlet g. See above.

g.run

The callable that g will run when it starts. After g started, this attribute no longer exists.

g.parent

The parent greenlet. This is writeable, but it is not allowed to create cycles of parents.

g.gr_frame

The current top frame, or None.

g.dead

True if g is dead (i.e. it finished its execution).

bool(g)

True if g is active, False if it is dead or not yet started.

g.throw([typ, [val, [tb]]])

Switches execution to the greenlet g, but immediately raises the given exception in g. If no argument is provided, the exception defaults to greenlet.GreenletExit. The normal exception propagation rules apply, as described above. Note that calling this method is almost equivalent to the following:

def raiser():

raise typ, val, tb

g_raiser = greenlet(raiser, parent=g)

g_raiser.switch()

except that this trick does not work for the greenlet.GreenletExit exception, which would not propagate from g_raiser to g.

Greenlets and Python threads

Greenlets can be combined with Python threads; in this case, each thread contains an independent “main” greenlet with a tree of sub-greenlets. It is not possible to mix or switch between greenlets belonging to different threads.

Garbage-collecting live greenlets

If all the references to a greenlet object go away (including the references from the parent attribute of other greenlets), then there is no way to ever switch back to this greenlet. In this case, a GreenletExit exception is generated into the greenlet. This is the only case where a greenlet receives the execution asynchronously. This gives try:finally: blocks a chance to clean up resources held by the greenlet. This feature also enables a programming style in which greenlets are infinite loops waiting for data and processing it. Such loops are automatically interrupted when the last reference to the greenlet goes away.

The greenlet is expected to either die or be resurrected by having a new reference to it stored somewhere; just catching and ignoring the GreenletExit is likely to lead to an infinite loop.

Greenlets do not participate in garbage collection; cycles involving data that is present in a greenlet’s frames will not be detected. Storing references to other greenlets cyclically may lead to leaks.

Tracing support

Standard Python tracing and profiling doesn’t work as expected when used with greenlet since stack and frame switching happens on the same Python thread. It is difficult to detect greenlet switching reliably with conventional methods, so to improve support for debugging, tracing and profiling greenlet based code there are new functions in the greenlet module:

greenlet.gettrace()

Returns a previously set tracing function, or None.

greenlet.settrace(callback)

Sets a new tracing function and returns a previous tracing function, or None. The callback is called on various events and is expected to have the following signature:

def callback(event, args):

if event == 'switch':

origin, target = args

# Handle a switch from origin to target.

# Note that callback is running in the context of target

# greenlet and any exceptions will be passed as if

# target.throw() was used instead of a switch.

return

if event == 'throw':

origin, target = args

# Handle a throw from origin to target.

# Note that callback is running in the context of target

# greenlet and any exceptions will replace the original, as

# if target.throw() was used with the replacing exception.

return

For compatibility it is very important to unpack args tuple only when event is either 'switch' or 'throw' and not when event is potentially something else. This way API can be extended to new events similar to sys.settrace().

C API Reference

Greenlets can be created and manipulated from extension modules written in C or C++, or from applications that embed Python. The greenlet.h header is provided, and exposes the entire API available to pure Python modules.

Types

Type name Python name

PyGreenlet greenlet.greenlet

Exceptions

Type name Python name

PyExc_GreenletError greenlet.error

PyExc_GreenletExit greenlet.GreenletExit

Reference

PyGreenlet_Import()

A macro that imports the greenlet module and initializes the C API. This must be called once for each extension module that uses the greenlet C API.

int PyGreenlet_Check(PyObject *p)

Macro that returns true if the argument is a PyGreenlet.

int PyGreenlet_STARTED(PyGreenlet *g)

Macro that returns true if the greenlet g has started.

int PyGreenlet_ACTIVE(PyGreenlet *g)

Macro that returns true if the greenlet g has started and has not died.

PyGreenlet PyGreenlet_GET_PARENT(PyGreenlet g)

Macro that returns the parent greenlet of g.

int PyGreenlet_SetParent(PyGreenlet g, PyGreenlet nparent)

Set the parent greenlet of g. Returns 0 for success. If -1 is returned, then g is not a pointer to a PyGreenlet, and an AttributeError will be raised.

PyGreenlet *PyGreenlet_GetCurrent(void)

Returns the currently active greenlet object.

PyGreenlet PyGreenlet_New(PyObject run, PyObject *parent)

Creates a new greenlet object with the callable run and parent parent. Both parameters are optional. If run is NULL, then the greenlet will be created, but will fail if switched in. If parent is NULL, the parent is automatically set to the current greenlet.

PyObject PyGreenlet_Switch(PyGreenlet g, PyObject args, PyObject kwargs)

Switches to the greenlet g. args and kwargs are optional and can be NULL. If args is NULL, an empty tuple is passed to the target greenlet. If kwargs is NULL, no keyword arguments are passed to the target greenlet. If arguments are specified, args should be a tuple and kwargs should be a dict.

PyObject PyGreenlet_Throw(PyGreenlet g, PyObject typ, PyObject val, PyObject *tb)

Switches to greenlet g, but immediately raise an exception of type typ with the value val, and optionally, the traceback object tb. tb can be NULL.

更多信息請查看IT技術專欄

更多信息請查看技術文章
易賢網手機網站地址:python greenlet
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026上岸·考公考編培訓報班

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久
亚洲高清视频的网址| 一区二区三区日韩精品视频| 亚洲国产精彩中文乱码av在线播放| 久久婷婷人人澡人人喊人人爽| 亚洲免费激情| 亚洲最新在线| 妖精视频成人观看www| 91久久线看在观草草青青| 狠狠色狠狠色综合日日五| 精品99一区二区三区| 亚洲精品国产欧美| 日韩午夜免费| 亚洲人成亚洲人成在线观看| 亚洲电影网站| 在线视频一区二区| 欧美亚洲一区二区在线观看| 欧美在线1区| 美女亚洲精品| 国产欧美亚洲精品| 黄色欧美日韩| 久久av红桃一区二区小说| 欧美第一黄色网| 国产精品久久激情| 亚洲欧洲在线观看| 欧美一区二区精美| 欧美日韩另类在线| 国产丝袜一区二区| 香蕉亚洲视频| 国内一区二区在线视频观看| 新片速递亚洲合集欧美合集| 国产精品私人影院| 亚洲欧美日韩中文视频| 国产精品久久久久久久app| 亚洲国产欧美不卡在线观看| 久久影院午夜论| 亚洲人永久免费| 欧美日韩国产一区| 亚洲最新在线| 国产亚洲一级| 久热精品视频| 亚洲精品国偷自产在线99热| 亚洲免费在线播放| 国产精品一区二区a| 久久久亚洲国产天美传媒修理工| 国产精品a久久久久久| 国产精品99久久久久久有的能看| 国产精品第一页第二页第三页| 中文久久精品| 91久久黄色| 国产精品美女久久| 欧美高清hd18日本| 亚洲免费网址| 在线观看亚洲精品| 国产美女搞久久| 欧美黑人在线播放| 亚洲欧美日韩综合| 亚洲电影在线| 欧美日产一区二区三区在线观看| 在线观看视频日韩| 欧美午夜视频一区二区| 久久五月激情| 一本久久综合亚洲鲁鲁五月天| 国产精品久久久久久av下载红粉 | 一区二区三区 在线观看视频 | 国产一区二区三区奇米久涩| 久久婷婷国产综合精品青草| 亚洲午夜激情| 亚洲欧美成人| 欧美一区二区三区视频| 亚洲一区二区在线| 亚洲视频综合| 欧美一区二区三区视频| 亚洲欧美在线另类| 久久综合一区二区三区| 欧美超级免费视 在线| 免费观看成人鲁鲁鲁鲁鲁视频| 欧美成人午夜免费视在线看片| 欧美精品免费在线观看| 欧美日韩国产综合视频在线观看中文 | 亚洲一区在线观看免费观看电影高清| 亚洲精品麻豆| 一区二区三区欧美| 久久av资源网站| 国产精品高潮久久| 1769国内精品视频在线播放| 亚洲欧美一区二区三区在线| 亚洲欧美日本国产专区一区| 久久国产黑丝| 国产精品久久久久久户外露出 | 欧美精品乱人伦久久久久久| 欧美日韩亚洲一区二| 黑人极品videos精品欧美裸| 亚洲精品久久嫩草网站秘色| 欧美一区二区视频网站| 欧美二区在线播放| 国产综合久久久久久| 亚洲精品一区二区三区在线观看 | 午夜欧美不卡精品aaaaa| 欧美88av| 国产日韩欧美在线播放不卡| 一本色道久久综合亚洲精品按摩| 久久婷婷国产综合精品青草 | 欧美三级在线| 在线观看亚洲| 久久精品国语| 精久久久久久| 久久精品一区二区| 亚洲黄色一区二区三区| 欧美成人免费在线| 亚洲精品无人区| 欧美欧美全黄| 亚洲网站在线观看| 国产精品久久久久久福利一牛影视| 亚洲国产精品成人| 男人天堂欧美日韩| 国内精品久久久久久久97牛牛| 欧美在线一二三四区| 国产欧美日韩专区发布| 久久综合九色欧美综合狠狠| 国产视频亚洲精品| 欧美尤物巨大精品爽| 国产三区二区一区久久| 一本色道88久久加勒比精品 | 欧美人成在线视频| 一区二区激情视频| 国产精品专区第二| 欧美激情乱人伦| 久久国产一区二区| 亚洲精品一区二区在线| 欧美区国产区| 午夜在线电影亚洲一区| 狠狠色狠狠色综合人人| 西西人体一区二区| 一本色道久久综合亚洲二区三区| 国产欧美亚洲视频| 国产精品九色蝌蚪自拍| 欧美连裤袜在线视频| 久久亚洲精品欧美| 欧美一区二区视频观看视频| 99国产精品视频免费观看| 国产真实乱子伦精品视频| 欧美日韩国产在线看| 久久久久网站| 欧美一区亚洲| 久久精品国产99| 久久久久久久一区二区三区| 一区二区电影免费观看| 亚洲国产日韩一区| 精品成人一区二区三区| 久久免费视频一区| 久久午夜国产精品| 欧美不卡激情三级在线观看| 欧美激情一区二区三区四区 | 欧美精品激情| 欧美激情第三页| 欧美日韩 国产精品| 欧美图区在线视频| 久久久久综合| 国产精品欧美久久| 亚洲中无吗在线| 亚洲国产老妈| 欧美大片国产精品| 国产欧美91| 亚洲午夜影视影院在线观看| 欧美激情在线有限公司| 亚洲黄色免费| 欧美日韩三级| 亚洲精品一区中文| 午夜精品久久久久久久99樱桃| 久久综合一区二区| 亚洲电影免费观看高清完整版在线| 99在线|亚洲一区二区| 欧美成黄导航| 中文在线不卡视频| 欧美午夜a级限制福利片| 好男人免费精品视频| 欧美在线黄色| 国产精品一级在线| 午夜精品美女自拍福到在线 | 激情五月***国产精品| 一区二区三区日韩欧美精品| 一本色道久久精品| 国产麻豆精品久久一二三| 亚洲永久精品大片| 久久精品国产一区二区三区免费看 | 亚洲精品少妇网址| 欧美剧在线免费观看网站| 99视频精品在线| 午夜精品一区二区三区电影天堂| 看欧美日韩国产| 国产女人精品视频| 免费人成精品欧美精品| 一区二区不卡在线视频 午夜欧美不卡'| 国产精品永久入口久久久| 欧美精品久久天天躁| 久久精品国产精品亚洲精品| 国产伦精品一区二区三区高清 | 欧美午夜精品一区| 久久青草久久| 午夜亚洲性色福利视频|