首页 > 解决方案 > 使用 NPM Python Shell 回调 Electron

问题描述

所以这是我的代码,我正在使用 python sheel 来执行一个读取 RFID 标签的 python 脚本。python脚本工作正常,但我的问题是我想在python脚本执行后(当我将标签放在RFID阅读器上时)控制台.log(“测试”),而不是控制台首先记录“测试”,然后然后执行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");
}

标签: javascriptpythonnpmcallback

解决方案


async 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);
         })
         readrfid.end(function (err) { 
          console.log("testing"); 
         });
     }

推荐阅读