【问题标题】:How to run a python script in Node.js once the node.js application has been packaged using 'pkg .'使用“pkg”打包 node.js 应用程序后,如何在 Node.js 中运行 python 脚本。
【发布时间】:2021-03-05 03:50:26
【问题描述】:

我正在尝试使用 spawn child_process 运行 python 脚本。在运行 nodemon app.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()

app.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 脚本?

任何帮助将不胜感激!

【问题讨论】:

标签: javascript python html jquery node.js


【解决方案1】:

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"(或"transitionend",如果您使用CSS3 transition)分配一个侦听器 - 比这样做:

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

【讨论】:

  • @mbh16 您是否在不了解其作用的情况下从某处复制粘贴代码?
猜你喜欢
  • 2017-06-30
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-31
  • 1970-01-01
相关资源
最近更新 更多