Sep-23-2018, 05:44 PM
(Sep-22-2018, 12:31 PM)jomonetta Wrote: does the server able to execute the other independent script which uses cron?Can get some solution with cron to work,but as server is running there are several solution made for this APScheduler,Celery,schedule, ect...
Here example with APScheduler,it's nice as can add job for several functions.
It run with
BackgroundScheduler
so it will not interfef with main loop that server run.from flask import Flask, render_template, jsonify from apscheduler.schedulers.background import BackgroundScheduler import random import requests app = Flask(__name__) def parse_func(): response = requests.get('https://nghttp2.org/httpbin/get') r = response.json() lst = [r['url'], r['origin']] rand_value = random.choice(lst) return(rand_value) @app.route("/") def template_test(): return render_template('sh2.html', location=parse_func()) if __name__ == '__main__': scheduler = BackgroundScheduler() scheduler.add_job(parse_func, 'interval', seconds=15) scheduler.start() app.run(debug=True)So now calling
parse_func()
every 15-sec,longer eg hour=12
.Just to see values on client side use jinja,with a reload script.
sh2.html:
<!doctype html> <html> <head> <title>Some title</title> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" /> </head> <body> <p> {{ location }}</p> </body> <script> function timedRefresh(timeoutPeriod) { setTimeout("location.reload(true);",timeoutPeriod); } window.onload = timedRefresh(15000); </script> </html>Can also look at this post.