- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要有关子流程模块的帮助。这个问题听起来可能是重复的,而且我已经在很多方面看到过很多与之相关的文章。但即便如此我也无法解决我的问题。其过程如下:
我有一个C程序2.c,其内容如下:
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
while(1)
{
if(a==0) //Specific case for the first input
{
printf("%d\n",(a+1));
break;
}
scanf("%d",&a);
printf("%d\n",a);
}
return 0;
}
我需要编写一个Python脚本,它首先使用subprocess.call()编译代码,然后使用Popen打开两个进程来执行各自的C程序。现在,第一个过程的输出必须是第二个过程的输入,反之亦然。所以本质上,如果我的初始输入是 0,那么第一个进程输出 2,由第二个进程获取。它依次输出 3,依此类推无限。
下面的脚本是我的想法,但它是有缺陷的。如果有人可以帮助我,我将非常感激。
from subprocess import *
call(["gcc","2.c"])
a = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #Initiating Process
a.stdin.write('0')
temp = a.communicate()[0]
print temp
b = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #The 2 processes in question
c = Popen(["./a.out"],stdin=PIPE,stdout=PIPE)
while True:
b.stdin.write(str(temp))
temp = b.communicate()[0]
print temp
c.stdin.write(str(temp))
temp = c.communicate()[0]
print temp
a.wait()
b.wait()
c.wait()
最佳答案
如果你想要第一个命令的输出a
作为第二个命令的输入 b
依次 b
的输出是a
的输入 - 像蛇吃尾部一样的圆圈 - 那么你不能使用 .communicate()
循环:.communicate()
直到进程终止并且所有输出都被消耗后才会返回。
一种解决方案是使用命名管道(如果 open()
在这种情况下在您的系统上不会阻塞):
#!/usr/bin/env python3
import os
from subprocess import Popen, PIPE
path = 'fifo'
os.mkfifo(path) # create named pipe
try:
with open(path, 'r+b', 0) as pipe, \
Popen(['./a.out'], stdin=PIPE, stdout=pipe) as b, \
Popen(['./a.out'], stdout=b.stdin, stdin=pipe) as a:
pipe.write(b'10\n') # kick-start it
finally:
os.remove(path) # clean up
它模拟a < fifo | b > fifo
来自 @alexander barakin answer 的 shell 命令.
这是一个更复杂的解决方案,它通过 python 父进程传输数据:
#!/usr/bin/env python3
import shutil
from subprocess import Popen, PIPE
with Popen(['./a.out'], stdin=PIPE, stdout=PIPE, bufsize=0) as b, \
Popen(['./a.out'], stdout=b.stdin, stdin=PIPE, bufsize=0) as a:
a.stdin.write(b'10\n') # kick-start it
shutil.copyfileobj(b.stdout, a.stdin) # copy b's stdout to a' stdin
此代码连接 a
的输出为 b
的输入通过 OS 管道使用重定向(如 a | b
shell 命令所做的那样)。
要完成这个圆圈,b
的输出被复制到 a
使用 shutil.copyfileobj()
在父 Python 代码中输入。
此代码可能有 buffering issues :进程之间有多个缓冲区:C stdio 缓冲区、包装管道的 Python 文件对象中的缓冲区(由 bufsize
控制)。
bufsize=0
关闭 Python 端的缓冲,数据一旦可用就会被复制。当心,bufsize=0
可能会导致部分写入 - 您可能需要内联 copyfileobj()
并再次调用write(),直到所有读取的数据都写入。
调用setvbuf(stdout, (char *) NULL, _IOLBF, 0)
,使标准输出在您的 C 程序中进行行缓冲:
#include <stdio.h>
int main(void)
{
int a;
setvbuf(stdout, (char *) NULL, _IOLBF, 0); /* make line buffered stdout */
do {
scanf("%d",&a);
printf("%d\n",a-1);
fprintf(stderr, "%d\n",a); /* for debugging */
} while(a > 0);
return 0;
}
10
9
8
7
6
5
4
3
2
1
0
-1
输出是相同的。
由于 C 子程序的编写和执行方式,您可能还需要捕获并忽略 BrokenPipeError
a.stdin.write()
末尾出现异常和/或 a.stdin.close()
( a
进程可能已经死亡,而 b
中有未复制的数据)。
关于python-3.x - 往返 2 个 Python 子进程的循环管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39232164/
考虑以下示例: >>> from IPython.core.inputtransformer2 import TransformerManager >>> src = """\ ... foo = !
所以我构建 Django 应用程序已经有一段时间了,喝了很酷的东西:只使用 ORM,从不编写自定义 SQL。 一旦您拥有大量用户特定内容(即照片、 friend 、其他数据等),网站的主页(用户将花费
我想使用 Excel 的 XML Map Web 应用程序中来自服务器端 C# 的功能。 XML 映射使您能够将 XML 架构与工作簿相关联,并指定哪些单元格映射到架构的哪些部分。您可以从那里导入 X
Closed. This question needs to be more focused。它当前不接受答案。
我有一个简单但具有挑战性的算法问题要解决。 我有一个交易员 - 股票 - 日级别的数据集,我想识别数据中的往返行程。往返只是数据中的特定序列。也就是说,如果您随着时间的推移累积个人 i 的股票 s 持
是否有一种可靠的方法可以将 HTTP cookie 值精确往返一次? 例如 Set-Cookie: name=value; Max-Age=1; ...但 Max-Age 以增量秒为单位而不是 #re
随着 Swift 3 倾向于 Data而不是 [UInt8] ,我试图找出将各种数字类型(UInt8、Double、Float、Int64 等)编码/解码为 Data 对象的最有效/惯用方法。 有th
我有一个具有以下结构的小部件(它是“计划选择器”简历): 我的指令如下: plans.directive('resume', function()
我们观察到所有消息的 RTT Kafka 延迟较高。 这是我们的实际配置: 我们有 3 个 Kafka 代理 + 3 个 Zookeeper 在 EC2 服务器上的 Kubernetes 集群中运行
使用 Newtonsoft.Json 库,想象一下 public class Test { public Object Obj { get; set; } } 现在,尝试像这样序列化它 var
这里和网络上有很多文章,但这些文章都针对不同的 Objectify 版本,并且由于某种原因似乎不起作用。 我有一个实体,它引用另一个实体(例如,帐户实体引用用户实体): @Cache @Entity
问题:当 SubSonic 对象通过 Windows Communication Foundation 服务来回发送时,如何保留它们的“脏”? 即: 我有一个返回 SubSonic (2.2) 对象集
我想使用 golang 的 xml.MarshalIndent() 快速创建一个实用程序来格式化任何 XML 数据 但是this code package main import ( "enc
我需要有关子流程模块的帮助。这个问题听起来可能是重复的,而且我已经在很多方面看到过很多与之相关的文章。但即便如此我也无法解决我的问题。其过程如下: 我有一个C程序2.c,其内容如下: #include
在尝试创建一些 FFI 帮助程序代码时,我对 Box 这种看似简单的用法感到头疼。 当与具有字段的结构一起使用时,此处的示例似乎给出了 free(): invalidpointer 错误。 pub s
我有两个函数用于在 Blob 和字节数组之间进行转换: function arrayToBlob(data) { return new Blob(data); } function blobT
我一直在尝试将带有 INTO OUTFILE 的 mysql 表导出为 CSV 以便在 Excel 中进行编辑,然后使用 LOAD DATA INFILE 插入回 mysql。 我可以导出 ok,甚至
我正在 Web 服务中实现条件请求。后端可以轻松检索实体的最后修改日期,因此我发送 Last-Modified 并返回 If-Modified-Since。 RFC for HTTP Dates指定与
我正试图找到一种方法来完成 xsd 模式到数据存储区的往返,而工作量最小。 我使用 jaxb 从模式构建我的对象模型,现在我想基于 JPA(或 JDO 或其他?)存储这些对象。是否可以根据 JAXB
我正在使用 DataAnnotations、jQuery.validate 和 jquery.validate.unobtrusive 创建一个日期范围验证器。我已经阅读了以下内容: http://b
我是一名优秀的程序员,十分优秀!