简体   繁体   中英

Python shared queue - 2 different threads

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?

When you create a new process, it copies the current environment and re-creates it in the new environment. Once you create the process, those global variables are no longer shared.

However, the multiprocessing library has included ways to communicate between processes

You can still use a queue, but you'll have to use the multiprocessing Queue instead of your home-rolled one. Under the hood, each process has its own Queue object, and they're simply piping information back and forth between the processes to sync the queue state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM