import time
from flask import Flask, jsonify
from multiprocessing import Process, Value
app = Flask(__name__)
class Queue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
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
}
]
q = Queue()
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
q.enqueue('cat')
print("Size: " + str(q.size()))
return jsonify({'tasks': tasks})
def record_loop(loop_on):
while True:
if loop_on.value == True:
print("loop running")
q.enqueue('dog')
print("Size: " + str(q.size()))
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()
I have a global class called queue. The queue's data is shared between 2 different functions, but its not working.
Why is the queue in the function 'get_tasks()' size always equal 1? I thought queues are thread safe and they can be shared between different processes?
record_loopruns. What is the end goal?