How to run multiple bots with a single Node.js/Python Plan on OuiHeberg
In this tutorial, we will show you how to run multiple bots (or scripts) using a single plan on OuiHeberg. We will cover two methods: one using Node.js and another using Python. These approaches will allow you to execute multiple processes in parallel efficiently.
Part 1: For NodeJS bot hosting plans
Step 1: Create a new JavaScript file
Create a new JavaScript file, for example, launchBots.js
, in the same directory as your bot scripts.
Step 2: Write the code to launch the files in parallel
Copy the following code into your launchBots.js
file. This code uses the child_process
module, included by default with Node.js, to execute each script in parallel.
const { spawn } = require('child_process');
// List of files to launch
const filesToExecute = ['script1.js', 'script2.js', 'script3.js'];
// Function to launch the files in parallel
function runFilesInParallel() {
filesToExecute.forEach((file) => {
const childProcess = spawn('node', [file]);
// Handle output events
childProcess.stdout.on('data', (data) => {
console.log(`[${file}] stdout: ${data}`);
});
childProcess.stderr.on('data', (data) => {
console.error(`[${file}] stderr: ${data}`);
});
childProcess.on('close', (code) => {
console.log(`[${file}] child process exited with code ${code}`);
});
});
}
// Launch the files in parallel
runFilesInParallel();
Replace the file names 'script1.js'
, 'script2.js'
, and 'script3.js'
in the filesToExecute
array with the names of your bot scripts. For example, if you have two bots named 'myFirstBot.js'
and 'mySecondBot.js'
, the filesToExecute
array should look like this: ['myFirstBot.js', 'mySecondBot.js']
.
Step 3: Update the server configuration on OuiPanel
- Log in to your OuiPanel account.
- Navigate to the "Server Configuration" section.
- Look for the "JS Startup File" section.
- Replace the existing file name with
launchBots.js
.
This tells OuiPanel to run your new JavaScript file that will launch all your bots.
Part 2: For Python bot hosting plans
Step 1: Create a new Python file
Create a new Python file, for example, launch_bots.py
, in the same directory as your bot scripts.
Step 2: Write the code to launch the files in parallel
Copy the following code into your launch_bots.py
file. This code uses Python's subprocess
module to execute each script in parallel.
import subprocess
# List of files to launch
files_to_execute = ['script1.js', 'script2.js', 'script3.js']
# Function to launch the files in parallel
def run_files_in_parallel():
processes = []
for file in files_to_execute:
process = subprocess.Popen(['node', file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
processes.append((file, process))
for file, process in processes:
stdout, stderr = process.communicate()
if stdout:
print(f"[{file}] stdout: {stdout}")
if stderr:
print(f"[{file}] stderr: {stderr}")
print(f"[{file}] child process exited with code {process.returncode}")
# Launch the files in parallel
run_files_in_parallel()
Replace the file names 'script1.js'
, 'script2.js'
, and 'script3.js'
in the files_to_execute
array with the names of your bot scripts. For example, if you have two bots named 'myFirstBot.js'
and 'mySecondBot.js'
, the files_to_execute
array should look like this: ['myFirstBot.js', 'mySecondBot.js']
.
Step 3: Update the server configuration on OuiPanel
- Log in to your OuiPanel account.
- Navigate to the "Server Configuration" section.
- Look for the "Startup File" section.
- Replace the existing file name with
launch_bots.py
.
This tells OuiPanel to run your new Python file that will launch all your bots.
Conclusion
And there you go! You have successfully launched multiple bots using a single plan on OuiHeberg, whether with Node.js or Python. Each bot runs in its own process, so if one bot crashes, it won't affect the others. This method allows you to efficiently manage multiple scripts simultaneously, optimizing your server resources.
Feel free to share your experiences or ask questions in the comments below!
Nombre del autor
OUIHEBERG SARL
Categorías
Tutoriels
Fecha
28/06/2024