Flask在向自己发送发布请求时挂起 [英] Flask hangs when sending a post request to itself

查看:191
本文介绍了Flask在向自己发送发布请求时挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从自己的视图发送一个请求到我的Flask应用程序,但它挂起,直到我杀死服务器。如果我在JavaScript中执行请求,它可以正常工作。为什么不从Python代码工作?

  from flask import蓝图,render_template,abort,request,Response,session,来自jinja2的url_for 
从flask.ext.wtf中导入TemplateNotFound
$ b $ import从wtforms中导入
import BooleanField,TextField,PasswordField

导入请求

login = Blueprint('login',__name__,template_folder ='templates')

LoginForm(表单):
email = TextField('Email')
密码= PasswordField('密码')

@ login.route('/ login',methods = ['GET','POST'])
def _login():
$ b form = LoginForm(request.form,csrf_enabled = False)
$ b $ if form.validate_on_submit():
返回requests.post(request.url_root +'/ api / login',data = {test:True})


return render_template('login.html',form = form)


解决方案

Flask's developme nt服务器默认是单线程的。它一次只能处理一个请求。发出请求阻塞,直到收到响应。您的Flask代码在一个线程中发出请求,然后等待。没有其他线程来处理这个第二个请求。所以请求永远不会完成,并且原始请求将永远等待。

在dev服务器上启用多个线程或进程以避免死锁并解决当前的问题。

  app.run(threaded = True)
#或
app.run(processes = 2)$ b $然而,从应用程序中向应用程序提交完整的HTTP请求不应该是必要的,并且表明更深的设计问题。例如,观察内部请求将无法访问客户端浏览器上的会话。提取公共代码并在内部调用它,而不是发出新的请求。

I'm trying to send a post request to my Flask app from one of its own views, but it hangs until I kill the server. If I do the request in JavaScript, it works fine. Why doesn't it work from the Python code?

from flask import Blueprint, render_template, abort, request, Response, session, url_for
from jinja2 import TemplateNotFound

from flask.ext.wtf import Form
from wtforms import BooleanField, TextField, PasswordField

import requests

login = Blueprint('login', __name__, template_folder='templates')

class LoginForm(Form):
    email = TextField('Email')
    password = PasswordField('Password')

@login.route('/login', methods=['GET', 'POST'])
    def _login():

    form = LoginForm(request.form, csrf_enabled=False)

    if form.validate_on_submit():
        return requests.post(request.url_root + '/api/login', data={"test": True})


    return render_template('login.html', form=form)

解决方案

Flask's development server is single-threaded by default. It can only handle one request at a time. Making a request blocks until it receives the response. Your Flask code makes a request in the one thread, and then waits. There are no other threads to handle this second request. So the request never completes, and the original request waits forever.

Enable multiple threads or processes on the dev server to avoid the deadlock and fix the immediate problem.

app.run(threaded=True)
# or
app.run(processes=2)

However, making a full HTTP request to the app from within the app should never be necessary and indicates a deeper design issue. For example, observe that the internal request will not have access to the session on the client's browser. Extract the common code and call it internally, rather than making a new request.

这篇关于Flask在向自己发送发布请求时挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆