【问题标题】:How to call python from javascript (nodejs) program, without creating child process如何从javascript(nodejs)程序调用python,而不创建子进程
【发布时间】:2019-06-20 09:18:15
【问题描述】:

我在 nodejs 上运行硬件,并且我在 python3 中编写了机器学习代码。 我想从nodejs(javascript)调用python3程序并将数据作为参数传递给python程序。

我发现了一些方法,例如使用 spawn、python-shell 等,它们通过创建子进程来调用 python。

但我想从父进程本身调用 python,而不创建子进程。 意味着,只有进程,即父 nodejs 进程应该运行。

test.py

def someFunction():
  #some code here
  #print the arguments received.

someFunction ()

test.js

call test.py from this test.js and need to pass arguments to python program as well.
call test.py with arguments as ('xyz', 'abc')

我期望输出: xyz abc

在不创建和启动子进程的情况下找不到任何调用方式。

【问题讨论】:

  • Python 是 interpreted language,我认为没有办法在不启动解释器进程的情况下运行未编译的纯 Python 代码。

标签: javascript node.js python-3.x


【解决方案1】:

你不能。您正在生成一个外部程序,它需要一个自己的进程。


好吧,这并不完全正确。您可以用 JavaScript 编写一个完整的 Python 解释器或虚拟机(您可以在其中运行操作系统,然后在其中运行 Python),然后您可以完全在 Node.js 进程中运行它……但是这将是一项巨大的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 2011-05-28
    • 2014-08-22
    • 1970-01-01
    • 2019-10-24
    • 2017-09-26
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多