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

奈克3

首先,我将发布我的代码,

我将举例说明我面临的问题:

        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);
                }
            })          
    };

我希望生成子对象仅同步,因为我的python脚本将返回一些文件,每次执行python脚本后都必须读取该文件。但是现在它在完成python执行后只读取一次,然后立即在控制台上打印它,而不是在每次执行python脚本之后都打印一次。

蒙施

这是一个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)

关于以上脚本的注释,我使用print\n在每行之后追加一个字符的方法。如果您打算使用python的write文件,则不会添加换行符。stdoutsys.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(): 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用竖线字符| 与child_process生成

来自分类Dev

使用茉莉监视节点child_process

来自分类Dev

当child_process中的exec shell时,child_process中的stdout无法在stdin之前输出右行

来自分类Dev

使用node.js child_process调用python脚本

来自分类Dev

使用node.js child_process调用python脚本

来自分类Dev

在节点child_process中检测python异常

来自分类Dev

使用Child_Process的NodeJS-如何转义我的命令

来自分类Dev

仅使用pid向分叉的child_process传递消息

来自分类Dev

(已解决)通过child_process使用脚本+屏幕

来自分类Dev

使用pid向分叉的child_process传递消息

来自分类Dev

如何在 webpack 中使用 child_process

来自分类Dev

Ionic 3 - 使用电子的“child_process”

来自分类Dev

在javascript中使用child_process执行shell命令

来自分类Dev

(代码重构)shell 命令在 javascript 中使用 child_process

来自分类Dev

将stdout / stderr从child_process重定向到/ dev / null或类似的东西

来自分类Dev

Child_process处理带有回车符(\ r)的STDOUT流

来自分类Dev

使用child_process(node js)在后台运行python脚本

来自分类Dev

我应该如何使用 child_process 模块在 linux 中通信 python 和 node.js?

来自分类Dev

在不使用Webpack的情况下使用模块“ child_process”

来自分类Dev

在Windows下使用child_process生成或exec时编码错误

来自分类Dev

如何在node.js child_process exec命令中使用“&”?

来自分类Dev

需要使用TypeScript,SystemJS和Electron的nodejs“ child_process”

来自分类Dev

NodeJS在raspi上使用命令df -h从child_process获取输出

来自分类Dev

使用有效的HOME文件夹设置在NodeJS中生成child_process

来自分类Dev

Firefox插件。使用child_process与本机应用程序进行通信

来自分类Dev

为什么要在节点中使用child_process而不是集群?

来自分类Dev

如何构建docker以从Google App Engine中的Node.js child_process运行Python3?

来自分类Dev

守护被'fork'的'child_process'

来自分类Dev

child_process流背压

Related 相关文章

  1. 1

    使用竖线字符| 与child_process生成

  2. 2

    使用茉莉监视节点child_process

  3. 3

    当child_process中的exec shell时,child_process中的stdout无法在stdin之前输出右行

  4. 4

    使用node.js child_process调用python脚本

  5. 5

    使用node.js child_process调用python脚本

  6. 6

    在节点child_process中检测python异常

  7. 7

    使用Child_Process的NodeJS-如何转义我的命令

  8. 8

    仅使用pid向分叉的child_process传递消息

  9. 9

    (已解决)通过child_process使用脚本+屏幕

  10. 10

    使用pid向分叉的child_process传递消息

  11. 11

    如何在 webpack 中使用 child_process

  12. 12

    Ionic 3 - 使用电子的“child_process”

  13. 13

    在javascript中使用child_process执行shell命令

  14. 14

    (代码重构)shell 命令在 javascript 中使用 child_process

  15. 15

    将stdout / stderr从child_process重定向到/ dev / null或类似的东西

  16. 16

    Child_process处理带有回车符(\ r)的STDOUT流

  17. 17

    使用child_process(node js)在后台运行python脚本

  18. 18

    我应该如何使用 child_process 模块在 linux 中通信 python 和 node.js?

  19. 19

    在不使用Webpack的情况下使用模块“ child_process”

  20. 20

    在Windows下使用child_process生成或exec时编码错误

  21. 21

    如何在node.js child_process exec命令中使用“&”?

  22. 22

    需要使用TypeScript,SystemJS和Electron的nodejs“ child_process”

  23. 23

    NodeJS在raspi上使用命令df -h从child_process获取输出

  24. 24

    使用有效的HOME文件夹设置在NodeJS中生成child_process

  25. 25

    Firefox插件。使用child_process与本机应用程序进行通信

  26. 26

    为什么要在节点中使用child_process而不是集群?

  27. 27

    如何构建docker以从Google App Engine中的Node.js child_process运行Python3?

  28. 28

    守护被'fork'的'child_process'

  29. 29

    child_process流背压

热门标签

归档