2017-01-15 3 views
0

次のスクリプトは、手動で端末から実行すると完全に実行されます。haskell System.Processからpython-programを呼び出すと、IOError:[Errno 5] Input/output error "

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) 
[email protected]:~/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 

を私はエラーメッセージをGoogleで検索してために有益な何かを見つけることができませんでしたpythonまたはhaskellのいずれかです。 とにかく、エラーは私が

+1

haskellスクリプトがstdinを終了してプロセスを実行しています。代わりにコマンドラインパラメータとして情報を渡すことはできませんか? ( 'sys.argv [1]'を使って)。それはそれを解決するだろう。 –

+0

afaik sys.argv [1]は新しい入力を送信したくない場合にのみ機能します。 –

答えて

2

ハスケル・スクリプトにいくつかの忙しい待機を追加する場合は、プロセスがパイプであることを標準入力したい場合は、明示的に要求する必要が解消されない:the fine documentationを引用

(Just input, _, _, _) <- createProcess ((proc "python" ["mplayer.py"]) { std_in = CreatePipe }) 

createProcess returns (mb_stdin_hdl,mb_stdout_hdl,mb_stderr_hdl,ph) , where

  • if std_in == CreatePipe , then mb_stdin_hdl will be Justh, where h is the write end of the pipe connected to the child process's stdin .
  • otherwise, mb_stdin_hdl== Nothing

Similarly for mb_stdout_hdl and mb_stderr_hdl.

+0

は今クラッシュしません。まだ入力はsys.stdin.readlineに束縛されていません。 –

+0

にはまだ "hFlush input"がありませんでした。今すぐ動作します –

+0

@someidiot 'putStr input" song.mp3 \ n ">> hFlush input'の代わりに' hPutStrLn input "song.mp3" 'を使うことができます(バッファリングをブロックするために' input'を変更していない限り)。 –

関連する問題