I’m running a python script from a Node.js Express API endpoint. The script is designed to run even after Express responds to the user. The code works, but it’s alarmingly slow to initialize. It takes 4 minutes + before the python script starts running. When I run the script directly in the shell, it’s instant. When I deploy to a reserved VM and call the API, it’s instant. Basically, when I deploy my app with a reserved VM, I don’t have the issue. But when I use Autoscale, I do.
Here is a very simple excerpt of the code:
const path = require('node:path');
const { spawn } = require('node:child_process');
const express = require('express');
const router = express.Router();
router.get('/run_script', async (req, res) => {
try {
const options = {
cwd: path.resolve(`${__dirname}/python/scripts`),
shell: '/bin/bash',
timeout: 30 * 60 * 1000,
stdio: 'pipe',
detached: false,
};
const child = spawn('python', ['my_script.py'], options);
child.stdout.setEncoding('utf8');
child.stdout.on('data', async (data) => {
console.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', async (data) => {
console.error(data);
});
child.on('close', async (code) => {
console.log(`Closed with Code: ${code}.`);
});
res.status(200).json({ message: 'Success' });
} catch (err) {
res.status(500).json({ message: 'Error' });
}
});
Here is my .replit file:
modules = ["nodejs-20:v8-20230920-bd784b9", "python-3.11:v14-20231207-2f65342", "bash:v7-20240329-787bc7d"]
hidden = ["package-lock.json", ".pythonlibs"]
run = "npm run dev"
[gitHubImport]
requiredFiles = [".replit", "replit.nix", "package.json", "package-lock.json"]
[nix]
channel = "stable-23_05"
[unitTest]
language = "nodejs"
[deployment]
run = ["sh", "-c", "npm run prod"]
deploymentTarget = "cloudrun"
[[ports]]
localPort = 3000
externalPort = 80
[env]
REPLIT_KEEP_PACKAGE_DEV_DEPENDENCIES = "1"
Does anyone have ideas into why Autoscale is causing major hanging/latency?