0

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()
tomeda
  • 75
  • 1
  • 11
  • That's because there's one process handling the HTML requests and another process doing the work. You don't have code that would tell the worker process that new work has arrived. Once you start multiprocessing, they don't know of each other. You would need inter process communication. – Thomas Weller Mar 23 '20 at 15:19
  • `tasks` is copied into the second process, not shared with the parent process. Multiprocessing works by actually pickling that list to disk and then being read when the new process starts. If you want a task queue, use one of the many already available for python, some with direct flask integration. Edit: celery is the most popular solution – jordanm Mar 23 '20 at 15:19
  • @ThomasWeller Thanks for the hints. I followed this https://stackoverflow.com/questions/218935/how-do-you-share-data-between-a-parent-and-forked-child-process-in-python – tomeda Mar 23 '20 at 16:01
  • @ThomasWeller however it doesn't solve my problem, because I have to close the connection in order for the while block to keep running. But if I close connection it means I no longer will be able to pass data in the future. If I don't clsoe it will be stuck waiting for new data.. – tomeda Mar 23 '20 at 16:02
  • @jordanm Thanks for that, I tried using the Pipe for that but facing a blocker ,please see above comments – tomeda Mar 23 '20 at 16:03

0 Answers0