简体   繁体   English

尝试通过 Process Pipe 发送/接收到 python 包装器,C++ 代码

[英]Attempting to send/receive over Process Pipe to python wrapper, c++ code

I have a python wrapper holding some c++ code.我有一个包含一些 C++ 代码的 python 包装器。 In it is a function that I setup as a process from my python code.它是我从我的 python 代码中设置为进程的一个函数。 Its a while statement that I need to setup a condition for when it should shut down.它是一个 while 语句,我需要设置它何时应该关闭的条件。

For this situation , the while statement is simple.对于这种情况,while 语句很简单。

while(TERMINATE == 0)

I have data that is being sent back from within the while loop.我有从 while 循环内发回的数据。 I'm using pipe() to create 'in' and 'out' objects.我正在使用 pipe() 创建“输入”和“输出”对象。 I send the 'out' object to the function when I create the process.创建进程时,我将“输出”对象发送给函数。

fxn = self.FG.do_videosequence
(self.inPipe, self.outPipe) = Pipe()
self.stream = Process(target=fxn, args=(self.outPipe,))
self.stream.start()

As I mentioned, while inside the wrapper I am able to send data back to the python script with正如我所提到的,在包装器内部时,我可以将数据发送回 python 脚本

PyObject *send = Py_BuildValue("s", "send_bytes");
PyObject_CallMethodObjArgs(pipe, send, temp, NULL);

This works just fine.这工作得很好。 However, I'm having issues with sending a message to the C++ code, in the wrapper, that tells the loop to stop.但是,我在向包装器中的 C++ 代码发送消息时遇到了问题,该消息告诉循环停止。

What I figured I would do is just check poll(), as that is what I do on the python script side.我想我要做的只是检查 poll(),因为这就是我在 python 脚本方面所做的。 I want to keep it simple.我想保持简单。 When the system sees that there is an incoming signal from the python script it would set TERMINATE = 1. so i wrote this.当系统发现有来自 python 脚本的传入信号时,它会设置 TERMINATE = 1。所以我写了这个。

PyObject *poll = Py_BuildValue("p", "poll");

As I'm expecting a true or false from the python function poll().因为我期待 python 函数 poll() 的真假。 I figured "p" would be ideal as it would convert true to 1 and false to 0.我认为“p”是理想的,因为它将真转换为 1,假转换为 0。

in the loop I have在循环中我有

if(PyObject_CallMethodObjArgs(pipe, poll, NULL, NULL))
            TERMINATE = 1;

I wanted to use poll() as its non-blocking, like recv() is.我想使用 poll() 作为它的非阻塞,就像 recv() 一样。 This way I could just go about my other work and check poll() once a cycle.这样我就可以继续我的其他工作并在一个周期内检查 poll() 一次。

however, when I send a signal from the python script it never trips.但是,当我从 python 脚本发送信号时,它永远不会跳闸。

self.inPipe.send("Hello");

I'm not sure where the disconnect is.我不确定断开连接在哪里。 When I print the poll() request, I get 0 the entire time.当我打印 poll() 请求时,我一直得到 0。 I'm either not calling it correctly, and its just defaulting to 0. or I'm not actually generating a signal to trip the poll() call.我要么没有正确调用它,它只是默认为 0。要么我实际上没有生成一个信号来触发 poll() 调用。 Thus its always 0.因此它始终为 0。

Does anyone have any insight as what i am doing wrong?有没有人对我做错了什么有任何见解?

*****UPDATE****** *****更新******

I found some other information.我找到了一些其他信息。

PyObject *poll = Py_BuildValue("p", "poll");

should be应该

PyObject *poll = Py_BuildValue("s", "poll");

as I'm passing a string as a reference to the function im calling it should be referenced as a string.当我传递一个字符串作为对调用它的函数的引用时,它应该被引用为一个字符串。 It has nothing to do with the return type.它与返回类型无关。

From there the return of从那里返回

PyObject_CallMethodObjArgs(pipe, poll, NULL, NULL)

is a pyobject so it needs to be checked against a pyobject.是一个 pyobject,因此需要针对 pyobject 进行检查。 such as making a call to比如打电话给

PyObject_IsTrue

to determine if its true or false.以确定它是真是假。 I'll make changes to my code and if I have solution I'll update the post with an answer.我将对我的代码进行更改,如果我有解决方案,我将用答案更新帖子。

So I've been able to find the solution.所以我已经能够找到解决方案。 In the end I was making two mistakes.最后我犯了两个错误。 The first mistake was when I created the pyobject reference to the python function I was calling.第一个错误是当我创建对我正在调用的 python 函数的 pyobject 引用时。 I mistook the information and inserted a "p" thinking before reading the context.在阅读上下文之前,我误解了信息并插入了“p”思维。 So所以

PyObject *poll = Py_BuildValue("p", "poll");

should be应该

PyObject *poll = Py_BuildValue("s", "poll");

The second mistake was how I was handling the return value of第二个错误是我如何处理返回值

PyObject_CallMethodObjArgs(pipe, poll, NULL, NULL)

while its true that its calling a python object, it is not returning a simple true false value, but rather a python object.虽然它确实调用了一个python对象,但它不是返回一个简单的真假值,而是一个python对象。 So I specificly needed to handle the python object, by calling所以我特别需要通过调用来处理 python 对象

PyObject_IsTrue(Pyobject o)

with the return of the poll() request as the argument.以 poll() 请求的返回作为参数。 I now have the ability to send/recieve from both the python script and the C api contained in the wrapper.我现在可以从 python 脚本和包装器中包含的 C api 发送/接收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM