2

I'm trying to learn why a server does not create a second thread for the second request:

from flask import render_template, redirect, url_for, jsonify
from . import pm
from .forms import NewForm
import requests, json

@pm.route('/pm/index')
def index():
    return render_template('pm/index.html')

@pm.route('/pm/new', methods=['GET', 'POST'])
def newitem():
    form = NewForm()
    if form.validate_on_submit():
        url = 'http://127.0.0.1:5000/api/search'
        payload = {'some': 'data'}
        r = requests.get(url)
        print(r.status_code)
        return redirect(url_for('material.index'))
    return render_template('pm/new.html', form=form)

I have an external API that I would like to POST information to, after a user POSTs to @pm.newitem

To facilitate development, The endpoint is stubbed to hit a endpoint on my local server but it just hangs forever. Why is this the case, if the endpoint is responsive when I test it manually?

this http://127.0.0.1:5000/api/search responds with that:

{
  "results": [
    {
      "id": 1,
      "name": "record"
    },
    {
      "id": 2,
      "name": "record2"
    }
  ]
}

Note, if I run two instances of the server and point the request to port 3000, it works.

JZ.
  • 21,147
  • 32
  • 115
  • 192

1 Answers1

4

I think you've answered your own question. The server is single threaded: if you make a request to itself inside a request, it will hang as it cannot possibly complete the original one.

Running two instances fixes this, as the second cab respond to the inner request.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • But why doesn't the server create a second thread for the second request? – JZ. Aug 21 '15 at 19:20
  • Why should it? Unless specifically configured, the dev server is single threaded. – Daniel Roseman Aug 21 '15 at 19:34
  • 1
    to handle my use case. ;) – JZ. Aug 21 '15 at 19:40
  • @JZ - It most certainly can - you just have to specify that you want a threaded (or a multi-process) server instead of the normal fast-to-spin-up single-threaded development one. See the linked duplicate. – Sean Vieira Aug 21 '15 at 20:21
  • 5
    Clearly not a duplicate question in any reasonable sense. No one searching for this issue is going to to find the supposed duplicate question. They have to know the answer to JZ's question first before they can know to ask the natural follow-up. Related, but distinct. – Brant Aug 21 '15 at 20:44