Advertisement
dredder_gun

python-server

Jun 9th, 2017
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3.  
  4. import threading
  5. import requests
  6. from flask import Flask
  7. from functools import wraps
  8.  
  9. app = Flask(__name__)
  10.  
  11.  
  12. def delay(delay=0.):
  13.     def wrap(f):
  14.         @wraps(f)
  15.         def delayed(*args, **kwargs):
  16.             timer = threading.Timer(delay, f, args=args, kwargs=kwargs)
  17.             timer.start()
  18.         return delayed
  19.     return wrap
  20.  
  21.  
  22. class SetInterval():
  23.     def __init__(self, func, sec):
  24.         def func_wrapper():
  25.             self.t = threading.Timer(sec, func_wrapper)
  26.             self.t.start()
  27.             func()
  28.         self.t = threading.Timer(sec, func_wrapper)
  29.         self.t.start()
  30.  
  31.     def cancel(self):
  32.         self.t.cancel()
  33.  
  34.  
  35. def post_to_clojure():
  36.     try:
  37.         requests.post("http://192.168.0.51:3000/from-python/")
  38.     except (RuntimeError, TypeError, NameError):
  39.         print "Error while query request"
  40.  
  41.  
  42. @delay(5)
  43. def stop_it(timerToStop):
  44.     timerToStop.cancel()
  45.     return 'Requests are posted'
  46.  
  47.  
  48. @app.route('/to-clojure', methods=['GET'])
  49. def start_interval():
  50.     timer = SetInterval(post_to_clojure, 1)
  51.     stop_it(timer)
  52.     return 'Ok, it is done'
  53.  
  54. if __name__ == '__main__':
  55.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement