How to use shared queues with python flag Restful web services

I am new to python flask REST web services. I am trying to create a leisure web service that will have a shared queue, several threads will be constantly written to this queue on the server side, and finally, when the user calls the GET methods, the service should return the first element in the shared queue.

I tried to start developing this by first implementing a shared variable, the next is the code I used,

from flask import Flask app = Flask(__name__) count= 0 #Shared Variable @app.route("/") def counter(): count = count+1 return {'count':count} if __name__ == "__main__": app.run() 

But even the code above does not work. Then I use the cache for the shared variable, but that will not be the right way to implement the shared queue (my final goal). Please give me your advice.

+6
source share
2 answers

I am afraid that the thing you want to do is a little more complicated than that.

Flask (and other python WSGI systems) do not work in any thread - they usually need to create multiple threads and instances to handle requests that come in without blocking, or without multiple requests accessing the same "first task", the same time. Thus, global variables do not work, as in other simple single-threaded python scripts.

You need to somehow get access to the same single data queue for different processes.

This usually means outsourcing the data queue to an external database. One popular option is Redis. There is a good introduction for flask and redis for this:

http://flask.pocoo.org/snippets/73/

Hope this helps you in the right direction!

+6
source

You have a couple of errors in your example. Here is the version that works:

 from flask import Flask, jsonify app = Flask(__name__) count= 0 #Shared Variable @app.route("/") def counter(): global count count = count+1 return jsonify({'count':count}) if __name__ == "__main__": app.run() 

Two problems that you have in your version:

  • You failed to declare count global in your view function. Without a global declaration, the view function creates a local variable with the same name.
  • The response returned by the view function cannot be a dictionary; it must be a string or a Response object. I fixed this with jsonify() to convert a dict to a JSON string.

But note that this way of creating general meaning is not reliable. In particular, note that if you run this application under a web server that creates several processes, each process will have its own copy of the count value.

If you need to do this on a production server, I recommend that you use a database to store your shared values.

+1
source

Source: https://habr.com/ru/post/958714/


All Articles