Loop to run python scripts

Asked

Viewed 732 times

0

I have a loop process to run scripts in parallel, but the VPS that I will last does not allow to use parallelism, In that case I need to create a code like:

scripts = [
    'siteodonto.py',
    'siteponto.py',
    'sitemeta.py',
    ...
]


for i in scripts:
    python i

scripts has a list of all the script names I need to run. What I don’t know how to do and how to run the scripts inside this other. python i [here would be calling the script Indice 1 python siteodonto.py] When you finish, you take the other until you close the list.

  • What is your operating system?

  • The system is Linux

  • What is a "VPS that doesn’t allow parallelism"? VPS is "Vrtual Private Server - it’s a virtual machine - as you always have root access inside it, you can run as many processes as you want in parallel. (may have only 1 core, but can trigger multiple processes in parallel and let the operating system turn around)

  • It has only one core, I did it plus the processor Ram arrived at 100% and killed all VPS processes.

1 answer

3


You can simply run them on the terminal inside a file bash. To do this, create an extension file sh and include commands to run the extension files py one after the other. Finally, go to the terminal and turn: bash meuarq.sh

Example:

Python file 1:

#primeiro programa extensão py
print('Hello world!')

Python file 2:

#segundo programa extensão py
print('Hello world again!')

Bash file:

#!usr/bin/bash
python python_program1.py
python python_program2.py

In the terminal:

bash meuarq.sh

Output:

Hello world!
Hello world again!

Follow alternative ways to run both files:

Concomitant:

 #!usr/bin/bash
python python_program1.py &
python python_program2.py &

Conditional 1 (spins the second only if the first spins successfully):

#usr/bin/bash
python python_program1.py && python python_program2.py

Conditional 2 (only rotate the following if the previous one fails):

#usr/bin/bash
python python_program1.py || python python_program2.py

Important: both conditions accept chaining

Alternatively, you can use the functions, variables and objects created in the files programa1.py and programa2.py in another program. For this, just put these files in the folder in which you are working and import them as modules:

from programa1 import *
from programa2 import *
  • I need to keep scheduled in the crojobs in just one file, so I thought if there’s any way to do with the same python.

  • 1

    Why don’t you import the files as modules in the last py extension file and just run the last one? I will edit my reply to include this option

  • they are separate scripts, use for webscraping each is to make the webscraping on a website, did not find a way to make out that to have a file that called the others

  • 1

    but you will always have a file that calls the others, even if the way you initially imagined (with list) worked

  • Only one question, when running like this, python python_program1.py python python_program2.py knows if it runs one at a time or all at a time?

  • You can do it both ways. The way it is, it will do it one at a time. I will edit my reply to include the code to run concurrently

  • He ran everything at once here so I asked you. He can’t run everything at once because vps doesn’t support.

  • See if the editing I did helps you

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.