Allow only one flask request for a particular route
<p>I have a flask app which uses angularjs front end. I make the http request through $http service. As shown in the code below.</p>

<pre><code>$http.post('/updateGraph', $scope.graphingParameters).success(function(response) {
        $scope.graphingParameters.graph = response.graph;
        $scope.listUnits = JSON.parse(response.listUnits);
        $scope.myHTML = $sce.trustAsHtml($scope.graphingParameters.graph);
        $scope.showME = true;
    })
</code></pre>

<p>and the updateGraph function in flask is as follows.</p>

<pre><code>@app.route('/updateGraph', methods = ['POST'])
     def updateGraph():
           selectValues = request.json['selectValues']
           selectSelected = np.array(request.json['selectSelected']).tolist()
           if len(selectSelected) == 0:
                selectSelected = np.array([selectValues[1:3]]).tolist()
           fig, listUnits = plot_Stock_vs_Sales(selectSelected)
           graph = py_offline.plot(fig, include_plotlyjs=False, output_type='div', show_link=False)
           return json.dumps({ 'graph': graph, 'listUnits':listUnits.reset_index().to_json(orient='records')})
</code></pre>

<p>The problem is that suppose make the $http post from angular twice, The flask route is running twice. this is the code from the server.</p>

<blockquote>
  <p>
  Seconds:92
  127.0.0.1 - - [12/Sep/2016 09:46:35] "POST /updateGraph HTTP/1.1" 200 -
  Seconds:110
  127.0.0.1 - - [12/Sep/2016 09:47:02] "POST /updateGraph HTTP/1.1" 200 -</p>
</blockquote>

<p>I want to either make the $http post request to only allow one request or make flask run only one route per user. Is this possible through flask? if not what would be the best approach through angular?</p>

<p>You could look at implementing a task or work queue using something like Redis and <a href="http://python-rq.org" rel="nofollow"><code>python-rq</code></a>.</p>

<p>Essentially, when the route is run, rather than perform your work immediately, you queue a task (in this case, to update your graph) to run asynchronously. This way you can ensure that the graph is updated atomically, or using some other criteria of your choosing (for example only once every ten minutes).</p>

jquery python angularjs ajax flask