首页 > 解决方案 > 如何在 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

实际上我注意到没有更多的核心转储。是不是因为异常被捕获了?谢谢您的帮助。

标签: pythonc++boost-python

解决方案


推荐阅读