2016-09-05 7 views
3

最初に私は私が問題に直面しています何の例を与えるchild_process forループを使用してspawnSyncの反復処理のPythonの標準出力結果は

私のコードを、掲載します私のpythonスクリプトはいくつかのファイルを返すでしょう、それはpythonスクリプトを実行するたびに読む必要があります。しかし、今はpythonの実行が完了した後で一度読むことができます。そして、一度にコンソールに印刷してください。毎回pythonスクリプトを実行するたびに印刷する代わりに、ここで

+0

あなたのコードは私のために動作します。あなたのループボディは、あなたが思うところで終わると確信していますか?これは完全な実際のコードですか? – Bergi

+0

こんにちは返信ありがとうございます。私は私の質問を編集しました。問題を解決するのを手伝ってください。 – naik3

+1

2回目の 'spawnSync'呼び出しで例外がスローされるのでしょうか?または 'arrayleng.length'は' 1'ですか? – Bergi

答えて

1

はnodejs例です。ここで

var fs = require('fs') 
var checkThese = ['/Users/jmunsch/Desktop/code_scraps/1.json', '/Users/jmunsch/Desktop/code_scraps/2.json'] 

checkThese.forEach(function(checkThisPath){ 
    // Both of these will log first 
    console.log(`run first: ${checkThisPath}`) 
    var data = fs.readFileSync(checkThisPath, 'utf8') 
    var theJson = data 
    console.log(`run first: ${theJson}`) 

    // it might make sense that this would execute next 
    // but it doesn't 
    fs.readFile(checkThisPath, 'utf8', function(err, data){ 
     // this callback executes later 
     console.log(`run second: ${checkThisPath}`) 
     console.log(`run second: ${data}`) 
    }) 
}) 

は、Pythonスクリプトとnodejsスクリプトとそれらがどのように働くかもしれないの例です。

Pythonスクリプトlistdir.py:上記のスクリプトで

import os 
import sys 
for x in os.listdir(sys.argv[1]): 
    print(x) 

注意事項は、私はそれぞれの行の後に\n文字を追加printを使用しました。 writestdoutというファイルを使用する場合は、pythonのsys.stdout.writeを使用して改行文字を追加しません。

nodejsコード:

var child_process = require('child_process'); 
var spawnSync = child_process.spawnSync 

var checkThese = ['/Users', '/Users/jmunsch'] 

for (var i=0; i < checkThese.length;i++){ 
    var checkThisPath = checkThese[i] 
    console.log(`Checking: ${checkThisPath}`) 
    var oneScript = spawnSync('python',["listdir.py", checkThisPath]); 
    oneScript.output.forEach(function(buffer){ 
     if (buffer){ 
      // convert to string 
      var x = buffer.toString('utf8') 
      console.log(`typeof x: ${typeof x}`) 
      // split by new line character 
      var y = x.split('\n') 
      console.log(`typeof y: ${typeof y}`) 
      // turn it into an array 
      var z = Array.prototype.slice.call(y) 
      // iterate each line in the array 
      z.forEach(function(pythonOutput){ 
       console.log(`One python print(): ${pythonOutput}`) 
      }) 
     } 
    })  
} 

nodejsコードからの出力:

Checking: /Users 
typeof x: string 
typeof y: object 
One python print(): .localized 
One python print(): administrator 
One python print(): casperadministrator 
One python print(): jmunsch 
One python print(): Shared 
One python print(): 
typeof x: string 
typeof y: object 
One python print(): 
Checking: /Users/jmunsch 
typeof x: string 
typeof y: object 
One python print(): .account 
One python print(): .ansible 
One python print(): .bash_sessions 
One python print(): .cache 
One python print(): .CFUserTextEncoding 
One python print(): .config 
One python print(): .cups 
One python print(): .DS_Store 
One python print(): .eclipse 
One python print(): .gitconfig 
One python print(): .ipython 
One python print(): .lesshst 
One python print(): .lldb 
One python print(): .local 
One python print(): .m2 
One python print(): .netrc 
One python print(): .node-gyp 
One python print(): .node_repl_history 
One python print(): .npm 
One python print(): .nvm 
One python print(): .oh-my-zsh 
One python print(): .oracle_jre_usage 
One python print(): .p2 
One python print(): .profile 
One python print(): .putty 
One python print(): .python-eggs 
One python print(): .rediscli_history 
One python print(): .RSA 
One python print(): .sh_history 
One python print(): .ssh 
One python print(): .swift 
One python print(): .tooling 
One python print(): .Trash 
One python print(): .vagrant.d 
One python print(): .viminfo 
One python print(): .wget-hsts 
One python print(): .zcompdump-LM-SFA-11003286-5.0.8 
One python print(): .zsh-update 
One python print(): .zsh_history 
One python print(): .zshrc 
One python print(): Applications 
One python print(): Desktop 
One python print(): Documents 
One python print(): Downloads 
One python print(): eclipse 
One python print(): git 
One python print(): Library 
One python print(): Movies 
One python print(): Music 
One python print(): Pictures 
One python print(): Public 
One python print(): synced 
One python print(): THEBIGPIN 
One python print(): VirtualBox VMs 
One python print(): 
typeof x: string 
typeof y: object 
One python print(): 
+0

こんにちは返信ありがとうございます。私は私の質問を編集しました。あなたは問題を解決するために私を助けてください – naik3

+0

こんにちはjmunsch。コードに非常に感謝します。私は私のpythonスクリプトが実行するものを印刷することができます。しかし、制御は、spawnSyncがすべてのファイルがロードされたpythonスクリプトを完全に実行するまで、fs.readFile(次の行)を実行していません。たとえば、** arrayleng.length **が10の場合、** spawnSync **行でのみ停止し、10番目のファイルが実行されるとfs.readFileで始まります。手伝ってくれませんか。 1つのpythonスクリプトを実行すると、次の行に移動する必要があります。私はなぜforループが動作していないのですか – naik3

+0

@ naik3 spawnSyncが終了するまでnodejsコードがブロックするか待機するかは、 "spawnSyncで停止する"とは何を意味するのか分かりません。どの時点で結果/出力/標準出力。あなたの質問を更新し、Pythonスクリプトを含めることができますか?また、プロセスの実行を正確に説明する「擬似コード」を含めることもできます。いくつのPythonスクリプトがありますか? Pythonスクリプトはどのように見えますか? Pythonスクリプトの後に次の行に行くとどういう意味ですか? – jmunsch

関連する問題