我想知道如何使用
以下是一些"父/子"代码,显示了我如何使用 PIPES 将数据流发送给孩子。代码可能并不完美,但你明白了。注意底部的
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define STDIN_FILENO 0 /* Standard input. */ #define STDOUT_FILENO 1 /* Standard output. */ #define STDERR_FILENO 2 /* Standard error output. */ #define MAXLINE 4096 int main(void){ int n, parent_child_pipe[2], child_parent_pipe[2]; pid_t pid; char line[MAXLINE]; if (pipe(parent_child_pipe) < 0 || pipe(child_parent_pipe) < 0) puts("Error creating pipes...\ "); if ( (pid = fork()) < 0) puts("Error forking...\ "); else if (pid > 0) { /* PARENT */ close(parent_child_pipe[0]); close(child_parent_pipe[1]); while (fgets(line, MAXLINE, stdin) != NULL) { n = strlen(line); if (write(parent_child_pipe[1], line, n) != n) puts("write error to pipe...\ "); if ( (n = read(child_parent_pipe[0], line, MAXLINE)) < 0) puts("read error from pipe...\ "); if (n == 0) { puts("child closed pipe...\ "); break; } line[n] = 0; /* null terminate */ if (fputs(line, stdout) == EOF) puts("fputs error...\ "); } if (ferror(stdin)) puts("fgets error on stdin...\ "); exit(0); } else { /* CHILD */ close(parent_child_pipe[1]); close(child_parent_pipe[0]); if (parent_child_pipe[0] != STDIN_FILENO) { if (dup2(parent_child_pipe[0], STDIN_FILENO) != STDIN_FILENO) puts("dup2 error to stdin...\ "); close(parent_child_pipe[0]); } if (child_parent_pipe[1] != STDOUT_FILENO) { if (dup2(child_parent_pipe[1], STDOUT_FILENO) != STDOUT_FILENO) puts("dup2 error to stdout...\ "); close(child_parent_pipe[1]); } **if (execl("./child","child", (char *) 0) < 0)** puts("execl error...\ "); } } |
现在上面的"child"程序是用 C 语言编写的,它只是通过 STDIN 接收流,操作流,然后使用 STDOUT 将其发送回。
类似:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdlib.h> #include <string.h> #include <unistd.h> #define STDIN_FILENO 0 /* Standard input. */ #define STDOUT_FILENO 1 /* Standard output. */ #define STDERR_FILENO 2 /* Standard error output. */ #define MAXLINE 4096 int main(void){ int n; char line[MAXLINE]; while ( (n = read(STDIN_FILENO, line, MAXLINE)) > 0) { line[n] = 0; /* null terminate */ n = strlen(line); if (write(STDOUT_FILENO, line, n) != n) puts("write error"); } exit(0); } |
所以工作正常,但现在我希望能够用 Python / Lua 等任何脚本语言编写 CHILD。我该怎么做?我尝试过类似的东西:
if (execl("python 的路径", "test.py", (char *) 0) < 0)
但它似乎只是挂起等待接收输入?
有人可以帮我解决这个问题吗?我假设 PIPES 可以与可以从 STDIN 读取并发送回 STDOUT 的 Lua / Python 之类的任何东西通信?
更新:
我现在做了一些小改动,这里是 Python 文件:"NullFilter.py",它简单地回显了它发送的内容:
2 3 4 5 6 7 8 9 10 11 12 13 14 | import sys class NullFilter: def execute(self): #Read data from STDIN... data = sys.stdin.read() #Write data to STDOUT... sys.stdout.write(data) exit(0) if __name__ == '__main__': nf = NullFilter() nf.execute() |
现在 C 代码调用它使用:
2 3 4 5 | if (execl("/usr/bin/python","./NullFilter.py","./NullFilter.py",NULL, (char *) 0) < 0) puts("execl error...\ "); ... |
当我现在运行它时,我可以在 STDIN 中输入文本,但必须按 CRTL-C 才能看到发生了什么:结果如下:
2 3 4 5 6 7 8 9 10 11 12 | hello hello again ^CTraceback (most recent call last): File"./NullFilter.py", line 17, in <module> nf.execute() File"./NullFilter.py", line 10, in execute debian@debian:~/Desktop/pipe example$ data = sys.stdin.read() KeyboardInterrupt debian@debian:~/Desktop/pipe example$ |
更新 2:
好的,所以我一直在玩,只是将 Python 代码从 "sys.stdin.read()" 更改为 "sys.stdin.readline()" ,它似乎工作在一定程度上,但根本不完美......
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import sys class NullFilter: def execute(self): #Read data from STDIN... data ="" for line in sys.stdin.readline(): data = data + line #Write data to STDOUT... sys.stdout.write(data) exit(0) if __name__ == '__main__': nf = NullFilter() nf.execute() |
这是一种解决方法,但一点也不完美。还有其他想法如何让标准输入读取无缓冲的流吗?我查看了 Python 中的 SELECT 模块:
http://docs.python.org/library/select.html
我也尝试将 "-u" 传递给 Python 以使其成为 "unbuffered" 但仍然没有运气 ;-(
但这肯定不会这么难吧?
林顿
在玩了很多游戏并尝试刷新所有内容之后,我意识到我需要关闭从父级到子级的 PIPE 的 STDOUT 端(当然是在写入管道之后)...
所以现在的代码是:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define STDIN_FILENO 0 /* Standard input. */ #define STDOUT_FILENO 1 /* Standard output. */ #define STDERR_FILENO 2 /* Standard error output. */ #define MAXLINE 4096 int main(void){ int n, parent_child_pipe[2], child_parent_pipe[2]; pid_t pid; char line[MAXLINE]; int rv; if (pipe(parent_child_pipe) < 0 || pipe(child_parent_pipe) < 0) puts("Error creating pipes...\ "); if ( (pid = fork()) < 0) puts("Error forking...\ "); else if (pid > 0) { /* PARENT */ close(parent_child_pipe[0]); close(child_parent_pipe[1]); while (fgets(line, MAXLINE, stdin) != NULL) { n = strlen(line); if (write(parent_child_pipe[1], line, n) != n) puts("write error to pipe...\ "); close(parent_child_pipe[1]); wait(&rv); if ( (n = read(child_parent_pipe[0], line, MAXLINE)) < 0) puts("read error from pipe...\ "); if (n == 0) { puts("child closed pipe...\ "); break; } line[n] = 0; /* null terminate */ if (fputs(line, stdout) == EOF) puts("fputs error...\ "); } if (ferror(stdin)) puts("fgets error on stdin...\ "); exit(0); } else { /* CHILD */ close(parent_child_pipe[1]); close(child_parent_pipe[0]); if (parent_child_pipe[0] != STDIN_FILENO) { if (dup2(parent_child_pipe[0], STDIN_FILENO) != STDIN_FILENO) puts("dup2 error to stdin...\ "); close(parent_child_pipe[0]); } if (child_parent_pipe[1] != STDOUT_FILENO) { if (dup2(child_parent_pipe[1], STDOUT_FILENO) != STDOUT_FILENO) puts("dup2 error to stdout...\ "); close(child_parent_pipe[1]); } if (execl("./NullFilter.py","./NullFilter.py", (char *) 0) < 0) puts("execl error...\ "); } } |
你可以看到"close(parent_child_pipe[1]);"在写完上面的 PIPE 之后,这是我必须做的关键部分。这将强制将流刷新到 Python 脚本、Lua 脚本、C 代码等......
在上面你会看到我正在执行一个 Python 脚本"NullFilter.py"....
注意:如果您运行上面的代码,它将适用于输入/输出的一次迭代,因为 Python 脚本在第一次测试后关闭了管道,但是您可以在此基础上构建必需品...
感谢所有的帮助,我从这个练习中学到了很多;-)
林顿
我相信您可以通过使用 shebang 行启动 python 文件来实现您想要做的事情,如下所示:
并确保它是可执行的,然后将脚本用作
中的可执行字段
见 http://linux.about.com/od/commands/l/blcmdl2_execve.htm:
Filename must be either a binary
executable, or a script starting with
a line of the form"#! interpreter
[arg]". In the latter case, the
interpreter must be a valid pathname
for an executable which is not itself
a script, which will be invoked as
interpreter [arg] filename.
在传递参数的情况下(理论上也是有效的),它看起来并不像 python 实际上正在执行脚本并完成它应该 - 而是打开一个交互式终端,这就是它挂起的原因 - 它希望你通过标准输入进行通信。尝试"test.py"的绝对路径,看看会发生什么,因为听起来 python 无法找到它。
实际上,还有另一种方法。没有理由不能通过管道将脚本逐行传递给解释器并以这种方式运行。
你试过冲孩子吗?
更新:
我尝试使用您的 C 代码和这 2 个脚本(perl 和 python),它们的行为与我机器上的 C 孩子完全一样
2 3 4 5 6 7 8 | use IO::Handle; while (<>) { print; flush STDOUT; } |
和
2 3 4 5 6 7 8 9 10 11 12 13 14 | import sys class NullFilter: def execute(self): while True: line=sys.stdin.readline() if not line: break sys.stdout.write(line) sys.stdout.flush() exit(0) if __name__ == '__main__': nf = NullFilter() nf.execute() |
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass