简体   繁体   English

Python 脚本静默停止

[英]Python Script Stops Silently

I'm running Python scripts as child processes, spawned using Nodejs.我正在运行 Python 个脚本作为子进程,使用 Nodejs 生成。

When running locally, or locally using Docker / Kube.netes installation, it works as expected and completes all functions in the script.在本地运行时,或者本地使用Docker/Kube.netes安装时,按预期运行,完成了脚本中的所有功能。 When running the container in Kube.netes Azure, the script silently stops / fails at just under 1 hour, without any exceptions or errors logged.在 Kube.netes Azure 中运行容器时,脚本在不到 1 小时的时间内静默停止/失败,没有记录任何异常或错误。

Memory & CPU usage stays below 30% max, container as a whole doesn't fail. Memory & CPU 使用率保持在 30% 以下,容器作为一个整体没有失败。 When running ps -fA | grep python运行时ps -fA | grep python ps -fA | grep python I can see the script running after it has been spawned. ps -fA | grep python我可以看到脚本在生成后正在运行。 Script doesn't show anymore after it fails / stops silently.脚本在失败/静默停止后不再显示。 The 'exit' and 'close' events within Nodejs for the spawned processes do not fire. Nodejs 中生成的进程的“退出”和“关闭”事件不会触发。

Any advice on how to troubleshoot would be much appreciated.任何有关如何排除故障的建议将不胜感激。

EDIT: Nodejs spawn编辑:Nodejs 产卵

import {/* inject, */ BindingScope, injectable} from '@loopback/core';

const path = require('path');

const spawn = require('child_process').spawn;

@injectable({scope: BindingScope.TRANSIENT})
export class PythonService {
  constructor() {} 
  stopPython(valuationId) {}

  executePython(id: string) {
    const filepath = path.resolve(process.env.PY_PATH);

    const ls = spawn('python', [filepath, id]);

    ls.stdout.on('data', function (data) {
      console.log('stdout: ' + data.toString());
    });

    ls.stderr.on('data', function (data) {
      console.log('stderr: ' + data.toString());
    });

    ls.error.on('error', function (data) {
      console.log('error: ' + data.toString());
    });

    ls.on('exit', function (code) {
      console.log('child process exited with code ' + code.toString());
    });

    ls.on('close', code => {
      console.log(`child process exited with code ${code}`);
    });
  }
}

EDIT: Dockerfile编辑:Dockerfile

# Pull base image
FROM python:3.7-slim

# Set installation environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV NODE_VERSION=12.20.0

# Install NVM for later use to install Node and NPM
RUN apt-get update && apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

# Set work directory
WORKDIR /home/node/app

# Install python dependencies
COPY  requirements.txt /home/node/app/
RUN pip install -r requirements.txt
RUN pip install swifter

# Install node app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY  package*.json ./
RUN npm install

# Bundle app source code
COPY . .

# Build node app
RUN  npm run build

# Expose ports
EXPOSE ${DB_PORT}
EXPOSE ${API_PORT}
EXPOSE ${SOCKET_PORT}

CMD [ "node", "." ]

Python v 3.7.11 Nodejs v 12.20 Python v 3.7.11 节点 v 12.20

Unix was killing the Python processes due to high memory usage, I was able to find OOM errors in the system logs by using ssh into the pod, then using dmesg for the kill logs and ps aux --sort -pmem to see the memory usage in the pod.由于 memory 的高使用率,Unix 正在终止 Python 进程,我能够通过在 pod 中使用 ssh 在系统日志中找到 OOM 错误,然后使用dmesg作为终止日志并使用ps aux --sort -pmem查看 memory 的使用情况在豆荚里。

Reason for OOM was that the default memory allocated to Nodejs was considerably higher than the normal 2GB limit, which decreased the available memory for Python. Decreasing Nodejs memory allocation or removing exlusive Nodejs memory allocation solves the issue. OOM 的原因是分配给 Nodejs 的默认 memory 大大高于正常的 2GB 限制,这减少了可用的 memory 用于 Python。减少 Nodejs memory 分配或删除独占 Nodejs memory 分配可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM