如何等待我的 python 函数的 return 以及如何使用我的控制器 nodejs return 我的响应主体中的数据?

How to wait the return of my python function and how to return the data in my response body with my controller nodejs?

我正在尝试 运行 python 中的一个函数,它 return 是 PDF 文件中的值,我将文件上传到用 nodejs 编写的控制器中,我调用python 函数与 PythonShell。我希望我的控制器等待解决我的 python 函数,这样我就可以 return 主体中的那些值,以便我可以在我的视图中使用这些值。 我的问题是我如何等待以及如何从函数中提取数据。

在 Python.stdout.on 中我可以在 console.log 中打印数据,但我不能 return 数据到函数外部

此函数 python 是数据的示例,因为 return 该函数是一个数据帧。

  async store({ request, response }) {
    try {
      if (!request.file("file")) return;

      const upload = request.file("file", { size: "2mb" });

      const fileName = `${Date.now()}.${upload.subtype}`;

      await upload.move(Helpers.tmpPath("uploads"), {
        name: fileName
      });

      if (!upload.moved()) {
        throw upload.error();
      }

      const resized = await path.resolve(
        __dirname,
        "..",
        "..",
        "..",
        "tmp",
        "uploads",
        fileName
      );

      let options = {
        mode: "text",
        pythonOptions: ["-u"],
        scriptPath: path.resolve(__dirname, "..", "Service"),
        args: [resized]
      };

      const Python = PythonShell.run("pdfRead.py", options, function(
        err,
        results
      ) {
        if (err) throw err;
        // results is an array consisting of messages collected during execution
        console.log("results: %j", results);
        const data = results;
        return data

      });

      const dataPython = await Python.stdout.on("data", data => {
        console.log(`data: ${data}`);
        const aux = [data];
        return aux        
      });

      return response.status(200).send(dataPython.data);

    } catch (error) {
      return response
        .status(error.status)
        .send({ error: { message: "Erro no upload de arquivo" } });
    }
  }

import sys
import pandas as pd

path = sys.argv[1]


def get_file(path):
    data = pd.read_csv(path)
    return data


print(get_file(path))
sys.stdout.flush()

我解决了我的问题,看代码:D 我使用 promisify 将函数 PythonShell(callback) 转换为 promise。

async store({ request, response }) {
    try {
      if (!request.file("file")) return;

      const upload = request.file("file", { size: "30mb" });

      const fileName = `${Date.now()}.${upload.subtype}`;

      await upload.move(Helpers.tmpPath("uploads"), {
        name: fileName
      });

      if (!upload.moved()) {
        throw upload.error();
      }

      const resized = await path.resolve(
        __dirname,
        "..",
        "..",
        "..",
        "tmp",
        "uploads",
        fileName
      );

      let options = {
        mode: "text",
        pythonOptions: ["-u"],
        scriptPath: path.resolve(__dirname, "..", "Service"),
        args: [resized]
      };

      const readPdf = promisify(PythonShell.run);

      const pdf = await readPdf("extrator.py", options);

      const json = JSON.parse(pdf[0]);

      const file = await File.create({
        file: fileName,
        name: upload.clientName,
        type: upload.type,
        subtype: upload.subtype,
        data: json
      });

      return response.send(file.file);
    } catch (error) {
      return response
        .status(error.status)
        .send({ error: { message: "Erro no upload de arquivo" } });
    }