Oh sorry, I just saw this. Thank you. Yes I can:
Even though I set up tasks, the same thing happens when I run a
file/script right from the file, which launches a console. I have a
script I run that does a web search and captures what the top 10
results are to a log file. When I run the script manually or via a
task, I can see in my console page under processes running python3.10 -i /bin/pythonanywhere_runner.py
. This process will run for over an
hour or more and eventually it must stop because my other tasks run.
But that's too long for my scripts to run effectively, because as long as that process is running, any other script, including if I try to run the same initial one again, hangs and doesn't complete. However, if I manually kill the pythonanywhere_runner.py script, then run any script, it runs fine. With that said, any script I run (let's assume its the one I first mentioned), initiates the pythonanywhere_runner.py process.
I have tried to write in functions into muy script to kill that process before running, such as the code below but I'm only able to kill the process via the Console > Process > Kill process via the website.
import os
import time
import subprocess
def kill_runner():
process_name = "pythonanywhere_runner.py"
timeout = 10 # Time to wait in seconds
start_time = time.time()
while time.time() - start_time < timeout:
try:
# Get all processes matching the name
result = subprocess.check_output(["pgrep", "-fa", process_name])
processes = result.decode().strip().split("\n")
if not processes:
print(f"No processes found for {process_name}")
return
# Kill each process found
for process in processes:
pid = process.split()[0] # Extract PID
print(f"Killing process {process_name} with PID {pid}")
os.system(f"kill -9 {pid}")
# Wait a short time before verifying
time.sleep(2)
except subprocess.CalledProcessError:
# Process not found, break out of loop
print(f"All {process_name} processes terminated successfully.")
return
print(f"Timeout reached. Could not kill all {process_name} processes.")
if __name__ == "__main__":
kill_runner()
[formatted by admin]