这段Python代码显示了如何在Windows 10中调用某个进程并向其发送字符串命令,如何通过该进程的stdin,stdout管道读取其字符串响应:
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import *
>>> p = Popen("c:/python38/python.exe", stdin=PIPE, stdout=PIPE)
>>> p.stdin.write(b"print(1+9)\n")
11
>>> p.communicate()
(b'10\r\n', None)
>>>
如您所见,python.exe进程返回10作为对的答案print(1+9)
。现在我想在Pharo(或Squeak)中做同样的事情:在Windows 10 OS中-我想类似的东西,即简短,简单,可理解,确实有效。
我安装了OSProcess,ProcessWrapper(在Pharo中缺少它们,也很奇怪,我得到警告,他们未标记为Pharo 8.0,也没有检查它们在Pharo 8.0中不能正常工作,但可以),然后尝试使用ProcessWrapper,PipeableOSProcess(复制-从网络粘贴不同的代码段),等等-零成功!结果是:
有人会向我展示一个简单的工作示例,如何在一个循环中启动一个过程并向其发送命令,阅读答案,然后再次发送,依此类推-我计划在一个分离的线程中进行此类通信并将其用作某种服务,因为Pharo,Smalltalk通常缺少大多数绑定,所以接下来我将使用子流程通信,例如在过去的“好”时期...
我知道如何调用命令并获取其输出:
out := LibC resultOfCommand: 'dir ', aDir.
但我正在谈论另一种情况:与正在运行的进程进行交互通信(例如,使用SSH或类似上面示例中的python.exe之类的通信)。
PS。#pipe:mode
甚至有可能用LibC做到这一点?
首先,我PipeableOsProcess
可能在Windows上损坏了它。我已经尝试过了,它只是打开了一个命令行而已(没有冻结我的Pharo 8)。整体OSProcess
在我眼中无法正常工作。
因此,我LibC
尝试使用Windows无法正常工作。
我是一个定义对标准LibC的访问的模块。我可以在Linux和OSX下使用,但由于明显的原因在Windows下不可用:)
接下来是说Python的Windows支持可能比Pharo更好。
该解决方案更像是使用文件的变通方法,它是使用LibC
和#runCommand:
(我尝试提供一个与上面显示的示例类似的示例):
| count command result outputFile errorFile |
count := 9+1. "The counting"
command := 'echo ', count asString. "command run at the command line"
outputFile := 'output'. "a file into which the output is redirected"
errorFile := 'error'. "a file where the error output is redirected "
result := LibC runCommand: command, "run the command "
' >', outputFile, "redirect the output to output file"
' 2>', errorFile.
"reading back the value from output file"
outputFile asFileReference contents lines.
"reading back the value from the error file - which is empty in this case"
errorFile asFileReference contents lines.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句