在C++应用程序运行时如何在后台启动嵌入式Python XMLRPC服务器?

2024-09-29 18:34:33 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我想开发一个测试程序,用嵌入式Python测试C++应用程序。这是我想修改的一个例子:https://github.com/skebanga/py_embed。你知道吗

我想做的是,而不是硬编码5中测试.py. 我想为不同的测试用例传递不同的值。有可能吗?你知道吗

修改前:上面例子的输出是

./my_program
created script
running
result=10

我的想法是将xmlrpcserver添加到测试.py接受输入。在我改变之后测试.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)

修改的测试.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()

我变了测试.py如上所述。 我还将try-and-catch添加到main()

 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

事实上,我注意到没有更多的核心转储。是因为捕获了异常吗? 谢谢你的帮助。你知道吗


Tags: inpyselfservermydeflineresult

热门问题