如何在 Node 中运行 python 脚本.js一旦 node.js 应用程序被打包使用 'pkg .'



我正在尝试使用spawn child_process运行python脚本。我已经设法在运行nodemon应用程序时使其工作.js但是我想打包我的节点应用程序并让它运行python脚本。一旦我使用以下方法打包了应用程序:

pkg .

它似乎能够运行代码,因为没有返回任何错误,但是 Python 脚本似乎不起作用。Python 脚本创建一些测试数据并将其保存到一个.csv文件中,然后节点将使用 Tail 查看该文件。

Python 脚本:

import random 
import time
import datetime
import math
import sys
cwd = ""
if len(sys.argv) == 2:
cwd = sys.argv[1]
print(cwd)
x = 0
y = 0
z = 0
while(True):
x = x + 0.1
y += 1
f =  open( cwd + "./public/data.csv", "a")

csv_writer = writer(f)

num1 = random.randrange(50,120)
num2 = random.randrange(50,120)
num3 = math.sin(x)

num4 = str(datetime.datetime.now().time()).split('.')[0] + "." + str(datetime.datetime.now().time()).split('.')[1][0:3]
print(num4)
csv_writer.writerow([num1,num2,num3,num4])
f.close()

应用程序中设置的节点尾部.js:

tail = new Tail("./public/data.csv");

这是我生成子进程的代码:

router.post("/testing/start", function (req, res) {
/**
* Start the python script. The python script will start generating the data.
* @memberof TestingRoutes.PythonScripts
* @function
* @name post
* @param '/testing/start' The url
* @instance
*/
python = spawn('python', ['./test.py', process.cwd()]);
testing_status = true;
res.send("success");
})

我的想法是,这与脚本的位置与可执行文件的位置有关。或者这与尾巴在看哪里有关。但是,我已经查看了活动监视器,以查看在启动脚本后是否有 Python 进程正在运行,并且似乎没有任何 python 程序正在运行。在节点可执行文件中甚至可以从节点启动 python 脚本吗?

任何帮助将不胜感激!

jQuery添加类的方法称为.addClass()
jQuery删除类的方法称为.removeClass()

$("#photo-container h1").addClass("animate-out") 

否则,在使用 JavaScript 的Element.classList构造函数之前,使用.get(0)[0]从 jQuery 对象中检索 HTMLElement。

$("#photo-container h1")[0].classList.add("animate-out")

第一行也是如此。
此外,如果要将侦听器分配给"animationend"(如果使用 CSS3transition,则"transitionend") - 请这样做:

const $H1 = $("#photo-container h1");
$H1.removeClass("animate animate-out");
$H1.on("animationend", function() {
// Do something once the CSS3 keyframes animation ends
if ( !$H1.is('.animate-out') ) {
$H1.addClass("animate-out");
} 
});

最新更新