python申请内存函数_Python Ctypes c函数的内存分配
我現在有一個python回調函數,它使用ctypes庫調用c函數。c函數需要一個指向一個結構的指針,例如animal_info_s,我實例化了這個結構并將它作為指向c函數的指針傳遞,它就可以工作了。我遇到的問題是,當我有多個線程調用回調時,我發現傳遞回來的信息在線程之間混淆了。在class animal_info_s(ctypes.Structure):
_fields_ = [('dog_type', ctypes.c_uint16),
('cat_type', ctypes.c_uint16),
('bird_type', ctypes.c_uint16),
('epoch_time', ctypes.c_uint16),
('more_information', ctypes.c_uint16)]
_mod = ctypes.cdll.LoadLibrary('bbuintflib.dll')
animal_info_s = animal_info_s()
get_animal_data = _mod.get_animal_data
get_animal_data.argtypes = [ctypes.POINTER(animal_info_s)]
get_animal_data.restype = ctypes.c_int
# Python Callback
def GetAnimalData():
animal_info_p = animal_info_s
res = get_animal_data(animal_info_p)
if (res != 0):
print("Failed to get animal info")
return
print ("Receive Time - %d\nDog: %d\nCat: %d\nBird:%d" %(animal_info_p.epoch_time,
animal_info_p.dog_type,
animal_info_p.cat_type,
animal_info_p.bird_type))
我想當我實例化這個結構時,它每次都使用相同的內存位置。如何為每個調用回調的線程創建一個新的內存位置?在
總結
以上是生活随笔為你收集整理的python申请内存函数_Python Ctypes c函数的内存分配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8位数控分频器的设计_eda设计数控分频
- 下一篇: mysql表只有frm文件_MYSQL数