带NPM Python Shell的电子回调

问题描述 投票:0回答:1

这是我的代码,我正在使用python sheel执行读取RFID标签的python脚本。 python脚本运行正常,但是我的问题是我想在执行python脚本后(将标签放在RFID阅读器上时)console.log(“ testing”),而是控制台首先记录“ testing”,然后然后执行python脚本。有什么办法可以解决此问题,回调还是其他?

function getDadosPulseira(){
    var {PythonShell} = require('python-shell')
    var path = require('path')

    var options = {
        scriptPath: path.join(__dirname, '../_engine/')//,
    }

    var readrfid = new PythonShell('readtag.py', options)

    readrfid.on("message", function(message) {
        swal({
            title: "Pulseira!",
            text: message,
            icon: "success",
            timer: 3000
        });
        console.log(message);
    })

    console.log("testing");
}
javascript python npm callback
1个回答
0
投票

Javascript是异步编程,因此您应该使用Promise,

   async function getDadosPulseira() {
    return new Promise(function (resolve, reject) {
        var {PythonShell} = require('python-shell')
        var path = require('path')

        var options = {
          scriptPath: path.join(__dirname, '../_engine/')//,
        }

        var readrfid = new PythonShell('readtag.py', options)

        readrfid.on("message", function(message) {
          swal({
            title: "Pulseira!",
            text: message,
            icon: "success",
            timer: 3000
         });
         console.log(message);
     })
    console.log("testing");
    });
 }
© www.soinside.com 2019 - 2024. All rights reserved.