I am referring to the example at Run while loop concurrently with Flask server
Basically,I have an endpoint for user to add to add task to the global tasks
list.
At the same time, I am running record_loop
in a separate thread. However, when I add a new task, in that separate thread record_loop
, it is not getting the updated tasks list. It will keep printing length of 2 no matter how many times I called the endpoint to add more tasks.
How do I get it to reference the updated list?
import time
from flask import Flask, jsonify
from multiprocessing import Process, Value
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/addtask/api/v1.0/tasks', methods=['POST'])
def add_tasks():
tasks.append({'id':100, 'title':'test'})
return jsonify({'tasks': tasks})
def record_loop(loop_on):
while True:
if loop_on.value == True:
print(len(tasks))
time.sleep(1)
if __name__ == "__main__":
recording_on = Value('b', True)
p = Process(target=record_loop, args=(recording_on,))
p.start()
app.run(debug=True, use_reloader=False)
p.join()