- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在使用 Python 2.7 开发 QGIS 的插件,在我真正对映射图层进行一些图像处理之前它工作正常。即使是收集栅格图层(比如 5k x 1k 部分)的 RGB 值这样的简单任务也需要一点时间(约 2 分钟)。如果必须的话,我可以接受,但是当我开始计算数据的简单指标(如熵)时,处理所需的时间量激增(我在大约 40 分钟无响应后停止了处理)。
我以前用 C++ 编写过 Entropy 代码,希望能够以这种方式处理数据。在做了一些研究之后,我发现 Python 可以使用 stdin 和 stdout 来传输数据,并在 http://ubuntuforums.org/archive/index.php/t-524072.html 的论坛中发现了以下示例。
使用此 Python 代码作为驱动程序
import subprocess
proc = subprocess.Popen("C:\Python27\PythonPipes.exe",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
state = "run"
while state == "run":
input = raw_input("Message to CPP>> ")
if input == "quit":
state = "terminate" # any string other than "run" will do
proc.stdin.write(input + "\n")
cppMessage = proc.stdout.readline().rstrip("\n")
print "cppreturn message ->" + cppMessage + " written by python \n"
这个c++代码作为数据处理器
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* args[]){
string python_message = "";
bool quit = false;
while (!quit){
cin >> python_message;
if (python_message == "quit"){
quit = true;
}
else if (python_message == "first"){
cout << "First Hello!" << endl;
}
else if (python_message == "second"){
cout << "Second Hello!" << endl;
}
else if (python_message == "third"){
cout << "Third Hello!" << endl;
}
else {
cout << "Huh?" << endl;
}
}
return 0;
}
此代码非常适用于文本数据。我可以整天用它来回传输文本。但我想做的是来回传递二进制数据,以便我可以将整数栅格图层数据从 python 发送到 c++ 进行处理,然后返回结果。
我环顾四周并尝试了将字节缓冲区放入
的各种组合proc.stdin.write(bufferdata)
并使用
fread(Arraypointer, 4,1,stdin)
用于在c++程序端接收缓冲区数据以及
fwrite(outArraypointer,4,1,stdout)
将数据通过管道传回 python,但尚未成功。我认为部分问题可能是文本版本使用了 cin 并等待 EOL 指示器。我不清楚如何在二进制数据的情况下做类似的事情。
我想知道如何修改上面的示例代码以将 int 从 python 程序发送到 c++ 程序。在 c++ 程序中增加 int,然后将该 int 发送回 python 程序。请记住,我将不得不一次使用数百万个整数来执行此操作,以防对您提出的解决方案提出警告。
如果这不起作用,我将转而让 python 写出一个二进制文件,供 C++ 代码读取,但如果可能的话,我真的很想使用这种方法。
提前感谢您的帮助。
更新 Roland 的解决方案是我需要的起点。对于后来的人,下面是我上面描述的代码的工作原型(prototype)。这是对罗兰的略微修改
Python 驱动程序:
import subprocess
proc = subprocess.Popen("C:\Python27\PythonPipes.exe",
stdin=subprocess.PIPE,stdout=subprocess.PIPE)
s1 = bytearray(10)
s1[0] = 65 #A
s1[1] = 66 #B
s1[2] = 67 #C
s1[3] = 68 #D
s1[4] = 69 #E
s1[5] = 70 #F
s1[6] = 71 #G
s1[7] = 72 #H
s1[8] = 73 #I
t = buffer(s1)
proc.stdin.write(t)
value = [0,0,0,0,0,0,0,0]
for i in range(8):
value[i] = ord(proc.stdout.read(1))
print "value i -> " + str(value[i])
proc.stdin.write('q')
proc.wait()
C++ 处理器
#include <stdio.h>
char increase;
int main(int argc, char **argv) {
for (;;) {
char buf;
fread(&buf, 1, 1, stdin);
if ('q' == buf)
break;
increase = buf + 1;
fwrite(&increase, 1, 1, stdout);
fflush(stdout);
}
return 0;
}
最佳答案
问题可能出在缓冲上:默认情况下,C stdio 缓冲所有写入 stdout 的内容,并仅在写入换行符时将缓冲区刷新回管道(行缓冲)。当您调用 fflush(stdout)
时问题消失了写完之后。您还可以通过 setvbuf
禁用(或控制)缓冲<stdio.h>
中定义的函数,例如使用 setvbuf(stdout, NULL, _IONBF, 0)
完全禁用缓冲。
我用以下两个程序测试了第一个变体:
import subprocess
proc = subprocess.Popen("./echotest",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
proc.stdin.write('abc')
message = proc.stdout.read(3)
print "return message ->" + message + " written by python \n"
proc.stdin.write('q')
proc.wait()
和一个小的 C 程序:
#include <stdio.h>
int main (int argc, char **argv) {
for (;;) {
char buf;
fread(&buf, 1, 1, stdin);
if ('q' == buf)
break;
fwrite(&buf, 1, 1, stdout);
fflush(stdout);
}
return 0;
}
请注意,您必须指定要从子进程读回多少字节,否则程序将阻塞,等待更多输出。如果这让您感到困扰,请尝试 this question 的解决方案之一。 .
关于python - 在 python 和 c++ 之间管道传输二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36748829/
我一直在做一些关于测量数据传输延迟的实验 CPU->GPU 和 GPU->CPU。我发现对于特定消息大小,CPU->GPU 数据传输速率几乎是 GPU->CPU 传输速率的两倍。谁能解释我为什么会这样
我将 ElasticSearch 用作我的 Post 模型的 Rails pet 项目应用程序的全文引擎。在我的 posts_controller.rb 索引操作中: def index
概述 流经网络的数据总是具有相同的类型:字节,这些字节如何传输主要取决于我们所说的网络传输。用户并不关心传输的细节,只在乎字节是否被可靠地发送和接收 如果使用 Java 网络编程,你会发现,某些时候当
我正在编写一些代码,以便将共享点从该页面转移到另一个页面: Server.Transfer("/DefectManagement/DefectList/default.aspx") 但是我遇到了这个问
我有这个泄漏,任何猜测?这个类有一些奇怪的引用。我的代码的任何地方都没有 contentobserver In com.example:1.5.0:27. com.example.ui.record
我听说过点对点内存传输并阅读了一些关于它的内容,但无法真正理解与标准 PCI-E 总线传输相比它的速度有多快。 我有一个使用多个 GPU 的 CUDA 应用程序,我可能对 P2P 传输感兴趣。我的问题
ftping 文件时,Transmit 中是否有忽略或过滤器列表?我希望它忽略上传 .svn 文件等。 最佳答案 是的。转到首选项并选择 Rules标签。在那里您可以定义要跳过哪些文件的规则。实际上,
我有以下片段来生成声音,在 while 循环中,我想动态更改它,以便它在声音生成期间创建不同频率的声音。 for(uint16_t i = 0; i < sample_N; i++) { da
我正在尝试使用 Delphi 2010 和 Indy 对 Web 服务进行概念验证。我此时的代码是: procedure TForm1.Log(const sEvent, sMsg: String);
我有一个 ActiveMQ JMS 代理,在端口 61616 上使用默认的 openwire TCP 传输公开。 我有许多远程客户端可以绑定(bind)到此代理来监听他们的消息。 如果我想打开 kee
reconnection strategies文档仅使用 JMS 示例,但是 FTP transport documentation确实说明了重新连接策略的使用,但没有任何细节或示例。 进一步,如果你
我有 2 个 TreeView,第一个填充有项目。 try { CheckBoxTreeItem treeRoot = new CheckBoxTreeItem("Root"); tr
在我为学校开发的一个网站上,用户输入他们的学校电子邮件和密码,如果他们已注册,则登录。如果没有,则会显示登录的第二部分,要求输入笔名称并确认密码。正因为如此,以及我复杂的业余 Django 编程,我有
我正在开发一个 Web 服务,我们在其中使用 LINQ-to-SQL 进行数据库抽象。当客户使用我们的网络服务时,对象被序列化为 XML,一切都很好。 现在我们希望开发我们自己的使用本地数据类型的客户
我应该创建一个名为“Backwards”的方法,该方法将列表从尾部横向到头部,但是当我运行代码时,它出现说(第 88 行)它找不到光标 = cusor.prev;象征。我需要在循环中再次设置上一个链接
给定像 Uint8Array 这样的类型化数组,似乎有两种方法可以通过 worker 传输它们。 选项 1 直接发送缓冲区并在接收端进行转换: 发件人:postMessage({fooBuffer:
在 PHP + jQuery 环境中,我和我的 friend 无法得出最佳解决方案。我们正在使用 Ajax 从数据库中获取数据。 解决方案 1 - Ajax 应该只传输数据,而不是 HTML 好处:我
大家好,非常感谢您的宝贵时间。 有一个 std::stringstream 需要传输到远程机器。网络库允许我用以下方法构建数据包: CreatePacket( const void * DATA, s
我正在使用 libcurl 通过 FTP 传输二进制文件 (.exe),并将其保存到本地文件。问题是文件传输后,它已被更改,不再是有效的 Win32 应用程序,因此无法运行。这是我的做法: CURL
各位程序员, 当我将它上传到我的 FTP 时,我的网站出现此错误:资源被解释为样式表,但使用 MIME 类型文本/纯文本传输 BlahBlahi
我是一名优秀的程序员,十分优秀!