简体   繁体   English

child_process spawnSync使用for循环迭代python stdout结果

[英]child_process spawnSync iterate python stdout results using for loop

First I will post my code, 首先,我将发布我的代码,

I will give an example of what I am facing problem: 我将举例说明我面临的问题:

        for(var i=0;i<arrayleng.length;i++){
        var oneScript = spawnSync('python',["/home/demo/mypython.py",arrayleng[i].path]);
        //console.log(String(oneScript.stdout));

        fs.readFile('/home/demo/' + arrayleng[i].filename + '.json','utf8',function(err,data){
                if(err){
                    console.log(err);
                }else{
                    console.log(data);
                }
            })          
    };

I want spawn child to synchronous only because, my python scripts will return some files, it has to read after each time it executes python script. 我希望spawn child只能同步,因为我的python脚本会返回一些文件,每次执行python脚本后都必须读取。 But now it is reading once after it completes execution of python.And printing it on console at once.Instead of printing after each time it executes python script. 但现在它在完成python的执行后会读取一次。并立即在控制台上打印它。而不是每次执行python脚本后打印。

Here is a nodejs example: 这是一个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}`)
    })
})

Here is an example of a python script and a nodejs script and how they might work. 以下是python脚本和nodejs脚本以及它们如何工作的示例。

The python script listdir.py : python脚本listdir.py

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

Notes on the above script, I used print which appends a \\n character after each line. 关于上面脚本的注释,我使用print在每行后附加一个\\n字符。 If you plan to write a file to stdout using python's sys.stdout.write it will not add a newline character. 如果您打算使用python的sys.stdout.write将文件write stdout ,则不会添加换行符。

The nodejs code: 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}`)
            })
        }
    })    
}

The output from from the nodejs code: 来自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(): 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM