Python Ctypes奇怪的行为 [英] Python Ctypes weird behavior
问题描述
我正在尝试为名为Virtual Paradise的应用程序设计一个机器人,并且将用于构建该机器人的SDK编译为共享库,因此我必须使用ctypes。
当我使用
导入线程
...
从ctypes导入CDLL时, CFUNCTYPE,c_char_p,c_int,c_void_p
vp = CDLL( libvpsdk.so)
vp.vp_string.restype = c_char_p
vp.vp_int.restype = c_int
...
类bot(threading.Thread):
def initBot(self):
...
实例= vp.vp_create()
...
EventFunc = CFUNCTYPE(无)
event_chat_func = EventFunc(self.event_chat)
vp.vp_event_set(实例,0,event_chat_func)
...
def event_chat(self):
打印你好
...
event_chat被正确调用并打印你好
但是当我使用此
导入线程
从ctypes导入聊天
...
导入CDLL,CFUNCTYPE,c_char_p,c_int,c_void_p
vp = CDLL( libvpsdk.so)
vp.vp_string.restype = c_char_p
vp.vp_int.restype = c_int
...
类bot(threading.Thread):
def initBot(self):
...
instance = vp.vp_create()
...
chat.VPSDK(vp,instance)
...
Chat.py:
从ctypes导入CFUNCTYPE
...
类VPSDK:
def __init __(self,vp,instance):
EventFunc = CFUNCTYPE(None)
event_chat_func = EventFunc(self.event_chat)
vp.vp_event_set(instance,0,event_chat_func)
def event_chat(self):
打印 Hello
...
我收到错误非法指令
我在做什么错!!我需要使用此单独的类,否则我的机器人的其他部分将失去功能。
您必须维护对在生命周期中可能会调用包装函数。请参见 15.16末尾的重要说明... 。 .1.17。 Python ctypes文档中的回调函数
一种方法是改用 self.event_chat_func
,在包含对象的整个生命周期内进行存储。 / p>
另外,创建 chat.VPSDK(vp,instance)
创建一个 chat的实例。 VPSDK
在下一行超出范围。在第一个示例中,您没有演示如何实例化 bot
,但是 VPSDK
对象的寿命并不长
I am trying to design a bot for an application called Virtual Paradise and the SDK that is given for building the bot is compiled into a shared library, therefore I have to use ctypes.
when I use
import threading
...
from ctypes import CDLL, CFUNCTYPE, c_char_p, c_int, c_void_p
vp = CDLL("libvpsdk.so")
vp.vp_string.restype = c_char_p
vp.vp_int.restype = c_int
...
class bot(threading.Thread):
def initBot(self):
...
instance = vp.vp_create()
...
EventFunc = CFUNCTYPE(None)
event_chat_func = EventFunc(self.event_chat)
vp.vp_event_set(instance, 0, event_chat_func)
...
def event_chat(self):
print "Hello"
...
event_chat gets called correctly and prints "Hello"
but when I use this
import threading
import chat
...
from ctypes import CDLL, CFUNCTYPE, c_char_p, c_int, c_void_p
vp = CDLL("libvpsdk.so")
vp.vp_string.restype = c_char_p
vp.vp_int.restype = c_int
...
class bot(threading.Thread):
def initBot(self):
...
instance = vp.vp_create()
...
chat.VPSDK(vp, instance)
...
Chat.py:
from ctypes import CFUNCTYPE
...
class VPSDK:
def __init__(self, vp, instance):
EventFunc = CFUNCTYPE(None)
event_chat_func = EventFunc(self.event_chat)
vp.vp_event_set(instance, 0, event_chat_func)
def event_chat(self):
print "Hello"
...
I get the error "Illegal instruction"
What am I doing wrong!? I need to use this separate class, otherwise other parts of my bot will loose functionality.
You must maintain a reference to the wrapped function for the lifetime it may be called. See the Important note... at the end of 15.16.1.17. Callback functions in the Python ctypes documentation.
One way is to use self.event_chat_func
instead, storing it for the lifetime of the containing object.
Also, creating chat.VPSDK(vp, instance)
creates an instance of chat.VPSDK
that goes out of scope in the next line. You don't demonstrate how bot
is instantiated in the first example, but the VPSDK
object doesn't live very long.
这篇关于Python Ctypes奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!