0

So basically I have a script that downloads python files from my webserver then runs them. What I'm looking for now is a way to detect if the downloaded python script is done running as then I want the original python script to run again to download the next file and so on. I'm trying to do this completely automated with no user input. I attempted to do this by checking to see if a text file has changed but realized that that would require any user attempting to upload their code to add a file change feature to their code.

Here's some of my code for reference:

import time

import wget
import os
import requests

def lookForFile():
    oldNumber=len(os.listdir("./upload"))
    while True:
        time.sleep(20)
        NumberOfFiles = len(os.listdir("./upload"))
        if NumberOfFiles != oldNumber:
            os.system('python ./upload/1.py')
            oldNumber = NumberOfFiles
def isRunning():
    file = "save.txt"
    with open(file) as f:
        lines = f.readlines()
        print(lines)
def notRunning():
    f = open("save.txt", "w")
    f.write("NotRunning")
    f.close()

    # open and read the file after the appending:
    f = open("save.txt", "r")
    print(f.read())

def updateRobot():
    url = "url/1.py"

    response = requests.get(url)
    angel = response.json()
    print(angel["currentNumber"])


def downloadScript():
    url = "url/1.py"

    wget.download(url, './upload/')

def main():
    pass
  • 1
    which OS are you using? You can make use of pgrep -lf to see if it is running. Or run your script by using `subprocess.run` which will block till it finishes and not os.system() – lllrnr101 Apr 10 '21 at 05:29
  • I'm currently testing this on a windows pc but plan on running it on my raspberry pi – Alexandr56 Apr 10 '21 at 05:45
  • actually os.system will also block. So what is your problem here? – lllrnr101 Apr 10 '21 at 06:35

2 Answers2

1

I think this will do what you're asking. It makes a list of all python process PIDs that already exist when the script is started. Then checks every second what current python processes are running that aren't on that list. Then you can add a callback when one of those processes leaves that list, for example where I put the "is done" print statement.

import time
import psutil
import threading


background_processes = [p.pid for p in psutil.process_iter() if 'python' in p.name()]


def find_nonbackground_processes():
    processes = []
    for p in psutil.process_iter():
        if 'python' in p.name():
            if p.pid not in background_processes:
                processes.append(p.pid)
    return processes


def wait_for_changes():
    processes = find_nonbackground_processes()
    print(processes)
    while True:
        current = find_nonbackground_processes()
        print(current)
        for p in processes:
            if p not in current:
                print(p, ' is done!')
        processes = current
        time.sleep(1)

        
t = threading.Thread(target=wait_for_changes)
t.start()

While this is running, if I start then stop an ipython process in the background, I get the following print statements:

[]
[]
[]
[14204, 15008]
[14204, 15008]
[]
14204  is done!
15008  is done!
[]
[]

PS this seems similar to Check if python script running from another python script linux but is probably sufficiently different that I wrote this more specific answer.

William
  • 381
  • 1
  • 8
0

Did you try using subprocess.run()?

Please see below for example on its blocking nature. That way you can process files directly without any need to check constantly.

I have two files:

  1. sleep_for_10_seconds.py
import time
time.sleep(10)
  1. call_using_subprocess.py
import subprocess
import time

file_to_run = 'sleep_for_10_seconds.py'
cmd_to_run = ['python', file_to_run]

before_running = time.time()
print(f'Current epoch: {before_running}')

subprocess.run(cmd_to_run)

print(f'Time elapsed since running: {time.time() - before_running}')

Running call_using_subprocess gives me following output:

Current epoch: 1618035414.6788576
Time elapsed since running: 10.054320335388184
lllrnr101
  • 2,288
  • 2
  • 4
  • 15