jdi*_*jdi 7 c++ python macros cython
我正在包装大量的C++函数,如果底层套接字连接丢失,可能引发异常.虽然我已经知道如何来包装我的"获取连接"功能重新建立连接和/或尝试在列表中的其他可用的服务器,我不能想出一个解决方案来创建一个try..except包装提供给80 + C++函数.
#-- client.pxd ---
cdef extern from "rpc/RpcService.h":
cdef cppclass RpcServiceClient:
void getProject(ProjectT&, Guid& id) nogil except +
cdef extern from "client.h":
cdef cppclass Client:
RpcServiceClient proxy() nogil
cdef Client* getClient() nogil except +
#-- module.pxd ---
cdef inline Client* conn() except *:
# wrap getClient() here with try..except if the
# connection was never established
cpdef inline get_project(Guid& guid):
cdef:
ProjectT projT # cpp object
Project project # cdef python class
# this would catch fine in my conn() wrapper
# if the connection had never been established
# the first time. But if the existing connection
# suddenly drops, it will be getProject() that
# raises the exception
conn().proxy().getProject(projT, guid)
project = initProject(projT)
return project
Run Code Online (Sandbox Code Playgroud)
关于如何将所有这些C++函数包装成类似的东西的任何提示try_call()
?如果这是纯python,我可以简单地做这样的事情:
def try_call(fn, *args, **kwargs):
# try fn(*args, **kwargs) and handle
try_call(conn().proxy().getProject, projT, guid)
Run Code Online (Sandbox Code Playgroud)
但显然我不能将这些cython函数作为python对象传递(或者我可以吗?).
或者像C++中的类似内容:
TRY_CALL_OR_RECONNECT
conn().proxy().getProject(projT, guid)
END_TRY_CALL_OR_RECONNECT
Run Code Online (Sandbox Code Playgroud)
你可能想看看装饰器
def try_wrapper(x):
try:
x()
except:
doSomethingElse()
@try_wrapper
def defYouWantToWrap():
doSomething()
Run Code Online (Sandbox Code Playgroud)
这可能不是最好的教程,但希望它能为您指明正确的方向
归档时间: |
|
查看次数: |
667 次 |
最近记录: |