0

I want make irrigation app on raspberry pi but i have problem with some how make communication between loop or schedule task

i try use something like this

import schedule
import time
import datetime
import uuid

from flask import Flask, request
from multiprocessing import Process

app = Flask(__name__)
t = None
job_timer = None

def run_job(id):
    """ sample job with parameter """
    global job_timer
    print("timer job id={}".format(id))
    print("timer: {:.4f}sec".format(time.time() - job_timer))
    job_timer = time.time()

def run_schedule():
    """ infinite loop for schedule """
    global job_timer
    job_timer = time.time()
    while 1:
        schedule.run_pending()
        time.sleep(1)

@app.route('/timer/<string:status>')
def mytimer(status, nsec=10):
    global t, job_timer
    if status=='on' and not t:
        schedule.every(nsec).seconds.do(run_job, str(uuid.uuid4()))
        t = Process(target=run_schedule)
        t.start()
        return "timer on with interval:{}sec\n".format(nsec)
    elif status=='off' and t:
        if t:
            t.terminate()
            t = None
            schedule.clear()
        return "timer off\n"
    return "timer status not changed\n"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

but is it problem to control schedule task because can't get some list of active schedule and separate stop each.

I thing best way make separate script with flask and loop but i don't know how make communication between script?

If is possible to provide some example communication between 2 script i would like to.

Roman
  • 83
  • 1
  • 1
  • 10
  • The global variable `job_timer` can not be shared between the subprocess and your main process. If you want to share the data between the `run_schedule` and `run_job`, consider storing the output from run_schedule in an instance of `multiprocess.Queue`. Check this answer for details: https://stackoverflow.com/a/11056415/2644759. – Philip Tzou Apr 24 '19 at 21:01
  • would a cron job be possible here? – gittert Apr 25 '19 at 09:38

0 Answers0