Base on Timer
import time
from threading import Timer
def on_time_handler(order_id=None, notify_url=None):
"""
https://docs.python.org/2/library/threading.html#threading.Timer
"""
# put data to rq queue will be a good choice.
# q.enqueue(notify_func, (order_id, notify_url)))
print 'On Time Handler: ', time.ctime(), order_id, notify_url
return
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/delay/<order_id>')
@app.route('/delay/<order_id>/<int:delay_time>')
def order_delay(order_id, delay_time=10):
now = time.time()
Timer(delay_time, on_time_handler, (order_id, '11111aaaa')).start()
print 'put order: {}, will run at {}'.format(order_id, time.ctime(now+delay_time))
return 'Add {}, notify at {}'.format(order_id, time.ctime(now+delay_time))
if __name__ == '__main__':
app.run(debug=True)
curl http://127.0.0.1:5000/delay/100
curl http://127.0.0.1:5000/delay/100
curl http://127.0.0.1:5000/delay/100
or
curl http://127.0.0.1:5000/delay/100/1
curl http://127.0.0.1:5000/delay/100/1
Test Result