检索无堆栈 Python Tasklet 绑定函数的返回值?
Stackless 专家,
我已经成功地在 Stackless Python 下创建了 tasklet(来自 Stackless 和 C 端)。
在我看来,为了在 Stackless 中创建一个 tasklet,您可以将任意 Python 可调用函数(函数)绑定到该 tasklet(以及所需的参数),因此绑定的可调用函数将作为一个 tasklet 运行。然而,任意可调用实际上可能具有对调用者来说很重要的返回值。但我还没有找到一种方法来检索作为微线程运行的绑定可调用对象的返回值。
在纯 Stackless Python 方面,我确实看到了一种称为 Micromanaging 的用法,它用管理函数包装原始函数,而管理函数又可以捕获原始函数的返回值并将其保存在某个地方以供使用。在其他上下文中使用。
不幸的是,我的特殊用例涉及从 C (C++) 端创建一个 tasklet,绑定到一个(可能阻塞的)Python 可调用对象,该可调用对象具有稍后要使用的重要返回值。看来在C端写这样一个Micromanaging函数不太可行,因为我还没有找到一种方法将C函数变成动态可调用的PyObject(不涉及模块表初始化等),并且使用静态无状态无论如何,C 函数(我假设原型必须是 PyObject* (PyObject*, PyObject*))在 C++ 世界中通常是一个坏主意。
Stackless C API 似乎也不包含用于检索微线程的返回值的适当函数。这是我必须在Python中编写上面提到的微观管理函数(可能是有状态的)的唯一选择,并提供一种方法来检索保存在某处的返回值(以有状态的方式,即不使用全局)变量)?或者我可以探索其他选择?
非常感谢,
林
P.S.据我所知,在C和操作系统级编程中,线程函数的返回值仅对退出代码有意义,并且线程函数将具有严格的函数原型,即C函数必须遵守一些严格的规则才能运行/通过线程。现在我们正在谈论 Python :)
Stackless Experts,
I have managed to create tasklets under Stackless Python (both from the Stackless and the C side).
It seems to me that in order to create a tasklet in Stackless, you bind an arbitrary Python callable (function) to the tasklet (as well as the required parameters), so the bound callable would be run as a tasklet. However, an arbitrary callable might actually have a return value that's important to the caller. But I have yet to see a way to retrieve the return value of the bound callable running as a tasklet.
On the pure Stackless Python side, I do see a usage idiom called Micromanaging, which wraps the original function with a managing function, which in turn could capture the return value of the original function and save it somewhere for use in some other context.
Unfortunately, my special use case involves creating a tasklet from the C (C++) side, binding to a (potentially blocking) Python callable which has an important return value to be used later. It seems that writing such a Micromanaging function on the C side is not very feasible, since I haven't found a way to turn a C function into a PyObject callable dynamically (not involving module table initialization, etc.), and using static stateless C function (I would assume the prototype has to be PyObject* (PyObject*, PyObject*)) is generally a bad idea under C++ world anyways.
The Stackless C API also seems not containing a proper function to retrieve the return value of a tasklet. Is it the only option that I have to write above mentioned Micromanaging function (could be stateful) in Python, and provide a way to retrieve the return value saved somewhere (in a stateful fashion, i.e. not using global variables)? Or there could be other options I could possibly explore?
Thank you very much,
Lin
P.S. I understand that on the C and operating system level programming, the return value of a thread function is only meaningful for exit code, and thread functions would have a strict function prototype, i.e. C functions have to abide by some strict rules to become runnable as/by a thread. And now we are talking about Python :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Stackless.com:
“...但是我还没有找到一种方法来检索作为微线程运行的绑定可调用对象的返回值。...”
我不知道是否有这样的方法。
“...重要的返回值稍后会用到。看来在 C 端编写这样的微观管理函数不太可行,因为我还没有...”
最简单的方法是定义包装C 中的 Python 函数代码作为字符串,并将其编译为可以作为 tasklet 运行的函数对象。下面是一个 Python 函数示例,C 部分我留给您。
例如
干杯,
理查德。
From Stackless.com:
"... But I have yet to see a way to retrieve the return value of the bound callable running as a tasklet. ..."
I do not know that there is one.
"...important return value to be used later. It seems that writing such a Micromanaging function on the C side is not very feasible, since I haven't ..."
The simplest way to do this would be to define the wrapping Python function code in C as a string, and to compile it into a function object that you can run as a tasklet. An example Python function follows, the C part I leave to you.
e.g.
Cheers,
Richard.