Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import threading
- import requests
- from flask import Flask
- from functools import wraps
- app = Flask(__name__)
- def delay(delay=0.):
- def wrap(f):
- @wraps(f)
- def delayed(*args, **kwargs):
- timer = threading.Timer(delay, f, args=args, kwargs=kwargs)
- timer.start()
- return delayed
- return wrap
- class SetInterval():
- def __init__(self, func, sec):
- def func_wrapper():
- self.t = threading.Timer(sec, func_wrapper)
- self.t.start()
- func()
- self.t = threading.Timer(sec, func_wrapper)
- self.t.start()
- def cancel(self):
- self.t.cancel()
- def post_to_clojure():
- try:
- requests.post("http://192.168.0.51:3000/from-python/")
- except (RuntimeError, TypeError, NameError):
- print "Error while query request"
- @delay(5)
- def stop_it(timerToStop):
- timerToStop.cancel()
- return 'Requests are posted'
- @app.route('/to-clojure', methods=['GET'])
- def start_interval():
- timer = SetInterval(post_to_clojure, 1)
- stop_it(timer)
- return 'Ok, it is done'
- if __name__ == '__main__':
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement