Usually if i guess right so is better for this task to look into something like
schedule,
APScheduler
Something a posted before,can start different task at long or short interval.
import schedule
import time, os
import subprocess
def make_file():
fn = 'test.out'
open(fn, 'a').close()
print(f'Making <{fn}>')
def del_file():
try:
fn ='test.out'
os.remove(fn)
print(f"Removed <{fn}>")
except FileNotFoundError:
print(f'Could not find <{fn}>')
def run_sh():
'''Example running shell script'''
subprocess.run(['bash', 'start.sh'])
schedule.every(8).seconds.do(make_file)
schedule.every(12).seconds.do(del_file)
while True:
schedule.run_pending()
time.sleep(1)
Output:
λ python remove_file.py
Making <test.out>
Removed <test.out>
Making <test.out>
Removed <test.out>
Making <test.out>
Making <test.out>
Removed <test.out>
Making <test.out>
So this is none blocking solution with Popen(as run can run stuff in Parallel).
Remember that
time.sleep()
is a blocking operation.
APScheduler
BackgroundScheduler dos separate (multi)thread automatic.
Or schedule
execute jobs in parallel?.