[英]“IOError: [Errno 5] Input/output error” when calling python-program from haskell System.Process
如果我從終端手動運行以下腳本,則該腳本可以完美運行。
import sys
import pygame
import pygame.mixer
def play(path):
pygame.mixer.music.load(path)
pygame.mixer.music.play(-1)
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
pygame.mixer.init()
while True:
path = sys.stdin.readline()[0:-1]
play(path)
每當我輸入路徑時,它就會開始播放該文件(並停止播放前一個文件)。
但是當我從這個haskell腳本中調用它時:
import System.Process
import GHC.IO.Handle
main = do
(Just input, _, _, _) <- createProcess (proc "python" ["mplayer.py"])
hPutStr input "song.mp3\n"
我收到以下錯誤消息:
Main: user error (Pattern match failure in do expression at Main.hs:6:9-29)
gernot@gernot-Aspire-5733Z:~/Dokumente/Projekte/python/music$ Traceback (most recent call last):
File "mplayer.py", line 14, in <module>
path = sys.stdin.readline()[0:-1]
IOError: [Errno 5] Input/output error
我搜索了錯誤消息,但沒有發現對python或haskell有用的任何東西。 無論如何,如果我在haskell腳本中添加了一些繁忙的等待,則錯誤仍然存在
如果您希望流程的stdin是管道,則必須顯式請求它:
(Just input, _, _, _) <- createProcess ((proc "python" ["mplayer.py"]) { std_in = CreatePipe })
引用好的文檔 :
createProcess
返回(
mb_stdin_hdl,
mb_stdout_hdl,
mb_stderr_hdl,
ph)
,其中
- 如果
std_in == CreatePipe
,則mb_stdin_hdl將為Just
h ,其中h是連接到子進程的stdin
的管道的寫端。- 否則, mb_stdin_hdl
== Nothing
對於mb_stdout_hdl和mb_stderr_hdl同樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.