
import time
import multiprocessing
from flask import Flask

app = Flask(__name__)
backProc = None

def testFun():
    print('Starting')
    while True:
        time.sleep(3)
        print('looping')
        time.sleep(3)
        print('3 Seconds Later')

@app.route('/')
def root():

    return 'Started a background process with PID ' + str(backProc.pid) + " is running: " + str(backProc.is_alive())

@app.route('/kill')
def kill():
    backProc.terminate()
    return 'killed: ' + str(backProc.pid)

@app.route('/kill_all')
def kill_all():
    proc = multiprocessing.active_children()
    for p in proc:
        p.terminate()
    return 'killed all'

@app.route('/active')
def active():
    proc = multiprocessing.active_children()
    arr = []
    for p in proc:
        print(p.pid)
        arr.append(p.pid)

    return str(arr)

@app.route('/start')
def start():
    global backProc
    backProc = multiprocessing.Process(target=testFun, args=(), daemon=True)
    backProc.start()
    return 'started: ' + str(backProc.pid)

if __name__ == '__main__':
    app.run()
