我想从twisted应用程序使用cTypesDLL。这里的小例子:from ctypes import *
from threading import Lock
lock = Lock()
dll = windll.LoadLibrary('mydll.dll')
l = [1,2,3]
def callback():
lock.acquire()
l.pop()
lock.release()
return 0
C_CALLBACK = CFUNCTYPE(c_int)
c_callback = C_CALLBACK(callback)
# this is a non blocking function call starts a hardware task that fires a callback upon completion
dll.registerCallback(c_callback)
while(True):
# in reality this block gets called from a twisted server application
lock.acquire()
l.append(l.pop() + 1)
lock.release()
dll有一个函数(dll.registerCallback),该函数接受ctypes回调函数,启动硬件事件,并在硬件指示硬件任务完成时触发回调。在
根据API文档:The callback function is called in a DAQmx thread.
他们试图在网络上解释什么是“DAQmx线程”:...your callback will be called and run in a DAQmx driver thread and will be run asynchronously (not in the same thread) in relation to your program.
完整的文档可以找到here。为了简单起见,我更改了示例中的函数签名。在
所以我想我们可以安全地假设dll正在生成一个线程。在
我拥有的锁是否可以确保回调函数在主循环中的pop操作的中间时不会试图访问列表l,反之亦然?或者这个方案只在使用使用threading库创建的线程时有效吗?这里推荐的做法是什么?在