带回调到python VM的pthread [英] pthread with callback to python VM
问题描述
假设我有一个python脚本,可通过ctypes加载共享库(SL).
Let's say I have a python script which loads a shared library (SL) through ctypes.
- SL设置了
pthread
T1 - python脚本通过SL配置回调,即python脚本从SL中调用对python可调用对象的引用的函数
- The SL sets up a
pthread
T1 - The python script configures callbacks through the SL i.e. python script calls functions from the SL with references to python callables
替代文本http://www.gliffy.com/pubdoc/1993061/L .jpg
现在,假设T1调用了回调"功能,那么以下假设是正确的:
Now, let's say T1 calls a "callback" function, are the following assumptions true:
- Python端的回调函数在T1的上下文中执行
- 我可以使用 队列 T1和Python VM
- 我需要在Python VM端轮询所说的
queue
- the callback function on the Python side is executed within the context of T1
- I can use a queue to communicate between T1 and the Python VM
- I would need to poll the said
queue
on the Python VM side
我了解线程,共享状态等的所有概念,但是我对Python的多线程方面还没有深入研究.由于目前还没有足够的适应层(ctypes),我恐怕会缺少该过程的一些关键方面.
I understand all the concepts of threading, shared state etc. but I haven't dug very deep on the multi-threading side with Python. Since there is an adaptation layer which I do not know enough at the moment (ctypes), I am afraid I'll be missing some key aspects of the process.
推荐答案
通常不需要轮询队列(您可以在Python端投入另一个线程来阻塞其上的.get
调用),但这不是必需的.大不了的.问题是,通过这样的安排,您可能会被 GIL 抓到–参见三个Wikipedia页面上的链接可对其进行充分的处理.
Polling the queue isn't normally necessary (you can devote another thread on the Python side to doing blocking .get
calls on it), but that's not a big deal. Problem is, with such an arrangement, you might get caught by the GIL -- see the three links from this wikipedia page for ample treatments thereof.
使用Python C API使用C(或 Cython )代码与C进行交互时,您至少可以相当简单地释放和获取GIL,希望避免死锁等问题;使用ctypes时,GIL操作会在C情境的回调中自动执行,因此,如果有其他任何发挥作用,则死锁是有风险的(由于事情不在您的控制范围之内,因此您无法轻易确保Djikstra的 应用了Banker算法 .
When you interface to/from C with C (or Cython) code using the Python C API, you can release and acquire the GIL pretty simply, at least, hopefully avoiding deadlocks and the like; with ctypes, GIL operations are automated in callback to/from C situations, so if there's any other lock in play a deadlock is a risk (since things are not within your control you can't easily ensure Djikstra's Banker Algorithm is applied).
这篇关于带回调到python VM的pthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!