python - 如何在 C++ 应用程序运行时在后台启动嵌入式 python xmlrpc 服务器?
问题描述
我想开发一个测试程序来测试使用嵌入式 python 的 C++ 应用程序。这是我要修改的示例:https ://github.com/skebanga/py_embed 。
我想要做的不是在 test.py 中硬编码 5。我想为不同的测试用例传递不同的值。可能吗?
修改前:上面例子的输出是
./my_program
created script
running
result=10
我的想法是将 xmlrpcserver 添加到 test.py 以接受输入。在我如下更改 test.py、重建并运行后,该线程由 xmlrpc 服务器持有。退出时会抛出如下错误。
有什么建议么?
./my_program
Listening on port 8000 ...
Hello
127.0.0.1 - - [18/Oct/2018 15:06:34] "POST /RPC2 HTTP/1.1" 200 -
^Cterminate called after throwing an instance of 'boost::python::error_already_set'
Aborted (core dumped)
修改后的 test.py
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
from SimpleXMLRPCServer import SimpleXMLRPCServer
class Script(object):
def __init__(self, ifc):
print 'created script'
self.ifc = ifc
def run(self):
print 'running'
self.ifc.execute(5)
self.startRPC()
def result(self, i):
print 'result={}'.format(i)
def startRPC(self):
server = SimpleXMLRPCServer(("", 8000), allow_none=True)
print ("Listening on port 8000 ...")
server.register_instance(self)
server.serve_forever()
我按照上面的更新更改了 test.py。我还在 main() 中添加了 try and catch
int main(){
try{
....
} catch (const bp::error_already_set&)
{
PyErr_Print();
return 1;
}
}
现在的输出是:
./my_program
created script
running
result=10
Listening on port 8000 ...
^CTraceback (most recent call last):
File "test.py", line 16, in run
self.startRPC()
File "test.py", line 31, in startRPC
server.serve_forever()
File "/usr/lib/python2.7/SocketServer.py", line 236, in serve_forever
poll_interval)
File "/usr/lib/python2.7/SocketServer.py", line 155, in _eintr_retry
return func(*args)
KeyboardInterrupt
实际上我注意到没有更多的核心转储。是不是因为异常被捕获了?谢谢您的帮助。
解决方案
推荐阅读
- python - 在将特征重要性转换为数据框时获取逗号分隔的输出。如何避免这种情况?
- jasper-reports - 抑制组中的重复值
- java - Java中的matlab bwmorph(img,'thin')实现出错了
- google-cloud-platform - 无法将“google_dns_record_set”从 count 转换为 for_each
- node.js - 如何在不使用端口 80 (http) 或端口 43 (https) 的情况下在 AWS EC2 上运行的 NodeJS 中编写 SSL 重定向?
- python - numpy printoptions 如何处理图像?
- java - 我的测试正在运行,然后我单击“运行为--> Maven clean”,现在当我单击“运行为--> Maven 测试”时,它不再工作了
- javascript - 我只想使用 js 或 jquery 复制输入框中的最后一个文本
- javascript - 为什么我的扩展程序在 Mozilla Firefox 上运行而不是在 Google Chrome 上运行?
- python - geojson 到 Elasticsearch:无法解析 [geo_shape] 类型的字段 [geometry.coordinates]