我将Python脚本作为子进程运行,使用Nodejs生成。
当在本地运行或在本地使用Docker / Kubernetes安装时,它按预期工作,并完成脚本中的所有功能。在Kubernetes Azure中运行容器时,脚本会在不到1小时内静默停止/失败,没有任何异常或记录错误。
内存和CPU使用率保持在最大30%以下,容器作为一个整体不会失效。在运行ps -fA | grep python时,我可以看到脚本在生成之后运行。脚本在失败/停止后不再显示。生成的进程在Nodejs中的“退出”和“关闭”事件不会触发。
如有任何关于如何排除故障的建议,将不胜感激。
编辑: 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}`);
});
}
}编辑: 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诉3.7.11 Nodejs诉12.20
发布于 2022-02-16 12:14:13
由于内存使用率高,Unix杀死了Python进程,我在系统日志中使用ssh查找OOM错误,然后使用dmesg对杀死日志和ps aux --sort -pmem查看吊舱中的内存使用情况。
OOM的原因是分配给Nodejs的默认内存大大高于正常的2GB限制,这减少了Python的可用内存。减少Nodejs内存分配或删除外部Nodejs内存分配解决了这个问题。
https://stackoverflow.com/questions/71058390
复制相似问题