Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使从Python调用的Haskell超级进程在一次使用后不关闭?_Python_Haskell_Subprocess_Eof - Fatal编程技术网

如何使从Python调用的Haskell超级进程在一次使用后不关闭?

如何使从Python调用的Haskell超级进程在一次使用后不关闭?,python,haskell,subprocess,eof,Python,Haskell,Subprocess,Eof,我有一个从Haskell编译的简单的.exe文件,它只是将它接收到的所有内容打印到stdout,然后再打印到stdin: module Main where main = do command <- getLine putStrLn command main 它第一次有效,但第二次无效,显然是因为它认为输入已完成: Sending command: Test1 Response: Test1 main.exe: <stdin>: hGetLine: end of

我有一个从Haskell编译的简单的.exe文件,它只是将它接收到的所有内容打印到stdout,然后再打印到stdin:

module Main where

main = do
  command <- getLine
  putStrLn command
  main
它第一次有效,但第二次无效,显然是因为它认为输入已完成:

Sending command: Test1
Response: Test1
main.exe: <stdin>: hGetLine: end of file


Sending command: Test2
Traceback (most recent call last):
  File ".\test.py", line 12, in <module>
    send_command("Test2")
  File ".\test.py", line 7, in send_command
    response = p.communicate(input=arg)[0].decode()
  File "C:\Python27\lib\subprocess.py", line 483, in communicate
    return self._communicate(input)
  File "C:\Python27\lib\subprocess.py", line 722, in _communicate
    self.stdin.write(input)
ValueError: I/O operation on closed file
并确保(以防万一)该.exe正确终止行:

module Main where

main = do
  command <- getLine
  putStrLn (command ++ "\n")
  main

我应该以某种方式“刷新”输入还是什么?

以下是您正在调用的函数的文档:

通信(输入=无,超时=无)

与进程交互:向stdin发送数据。从stdout和stderr读取数据,直到到达文件末尾等待进程终止

(我的重点)

如果你不想让
的所有其他东西都与
交流
的话,你可以直接写到
p.stdin
并从
p.stdout
读取

我不知道这是Python还是Haskell的错


这不是批评,而是一个有用的提示:错误在于没有阅读他们调用的函数文档的人。它是免费的,读吧

以下是您正在调用的函数的文档:

通信(输入=无,超时=无)

与进程交互:向stdin发送数据。从stdout和stderr读取数据,直到到达文件末尾等待进程终止

(我的重点)

如果你不想让
的所有其他东西都与
交流
的话,你可以直接写到
p.stdin
并从
p.stdout
读取

我不知道这是Python还是Haskell的错


这不是批评,而是一个有用的提示:错误在于没有阅读他们调用的函数文档的人。它是免费的,读吧

通信
实际上是这样做的,因为它传递了输入,并关闭了输入通道的一侧。如果我想继续与子流程交互,我应该使用什么来代替通信?您可以写入流程的
.stdin
,您可以从进程的
.stdout
中读取数据。
通信
实际上是这样做的,因为它传递了输入,并关闭了输入通道的一侧。如果我想继续与子进程交互,我应该使用什么来代替通信?您可以写入进程的
.stdin
,您可以从进程的
.stdout
中读取。是的,输入/输出流可能都已缓冲。您可以在Python端调用
p.stdin.flush()
Popen.stdin
的文档链接到
open()
,链接到内置文件类型,链接到模块。我不知道Haskell在这里的行为。@user2649762,在Haskell方面,我不确定在这种情况下默认的缓冲是什么。它可能是
LineBuffering
,在这种情况下,您应该没问题。如果仍有问题,则应导入
System.IO
。然后可以使用
hFlush stdout
在Haskell端显式刷新stdout,也可以调用
hSetBuffering stdout LineBuffering
hSetBuffering stdout NoBuffering
全局修改缓冲。在特殊情况下,您可能只需要
NoBuffering
,因为如果进行许多小的写入,它的性能会很差。是的,输入/输出流都可能被缓冲。您可以在Python端调用
p.stdin.flush()
Popen.stdin
的文档链接到
open()
,链接到内置文件类型,链接到模块。我不知道Haskell在这里的行为。@user2649762,在Haskell方面,我不确定在这种情况下默认的缓冲是什么。它可能是
LineBuffering
,在这种情况下,您应该没问题。如果仍有问题,则应导入
System.IO
。然后可以使用
hFlush stdout
在Haskell端显式刷新stdout,也可以调用
hSetBuffering stdout LineBuffering
hSetBuffering stdout NoBuffering
全局修改缓冲。在特殊情况下,您可能只需要
NoBuffering
,因为如果进行许多小的写操作,它的性能会很差。
from subprocess import Popen, PIPE, STDOUT

p = Popen(['main.exe'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)  

def send_command(arg):
    print("Sending command: "+arg)  
    p.stdin.write(arg+"\n")
    response = p.stdout.readline().decode()
    print("Response: "+response+"\n")
    return response

send_command("Test1")
send_command("Test2")
module Main where

main = do
  command <- getLine
  putStrLn (command ++ "\n")
  main
Sending command: Test1