zoukankan      html  css  js  c++  java
  • Flask【第十一章】:Flask中的CBV以及偏函数+线程安全

    Flask中的CBV以及偏函数+线程安全

    一、Flask中的CBV

    之前我们是用视图函数,Flask也有视图类,就像Django一样。看一看试图类怎么使用。

    步骤:

    1.先导入我们的flask模块,以及之后我们所需要的模块

    from flask import Flask
    from flask import views,render_template

    2.创建一个Flask实例

    app = Flask(__name__,template_folder="templates")

    3.创建一个视图类

    class Login(views.MethodView):
        methods = ["GET","POST"]
        decorators = ["be1","app.route"]
    
        def get(self):
            return render_template("login.html")
    
        def post(self):
            return "This is a post"

    注意:methods以及一些配置可以当做字段写在视图类中,也可以写在app的路由设置中

    4.创建url路由,注意一些配置,比如methods等等,可以在这里面进行配置,类似视图函数一样设置。

    注意:默认当没有定义methods时,视图类中所有的函数比如get、post,只要存在就都可以访问,如果设置了methods,那么就只能允许methods中的请求访问。

    app.add_url_rule("/",view_func=Login.as_view("my_login"))

    5.启动app

    if __name__ == "__main__":
        app.run(debug=True)

    看一下完整的代码:

    1.目录结构:

    2.f1.py内容:

    from flask import Flask
    from flask import views,render_template
    
    app = Flask(__name__,template_folder="templates")
    
    
    class Login(views.MethodView):
        methods = ["GET","POST"]
        decorators = ["be1","app.route"]
    
        def get(self):
            return render_template("login.html")
    
        def post(self):
            return "This is a post"
    
    app.add_url_rule("/",view_func=Login.as_view("my_login"))
    
    if __name__ == "__main__":
        app.run(debug=True)

    3.login.html代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="" method="post">
        用户名:<input type="text" name="user">
        密码:<input type="password" name="pwd">
        <input type="submit" value="登录">
    </form>
    </body>
    </html>

    4.访问浏览器

    点击登录后:

    二、偏函数+线程安全

    1.偏函数

    看一个例子:

    def ab(a,b):
        return a+b
    
    print(ab(1,2))
    
    
    ##结果为3
    from functools import partial
    
    def ab(a,b):
        return a+b
    
    par_ab = partial(ab,5)
    print(par_ab(6))
    
    #结果是11

    看出来了吗,下面的那个例子就是偏函数的例子。引用了functools模块中的partial

    解释:将5传值给a,然后在把ab函数赋给par_ab,这是par_ab(6)实际就是ab(5,6),所以等于11.这就是偏函数。

    2.线程安全

    看一个例子:我想要1秒内同时执行多多个任务

    import time
    import threading
    
    class Foo(object):
        pass
    
    foo = Foo()
    
    def add(i):
        foo.num = i
        time.sleep(1)
        print(foo.num,i)
    
    for i in range(20):
        th = threading.Thread(target=add,args=(i,))
        th.start()

    结果:

    D:python3.6.6python3.exe E:/python/Flasktest/flask1/f1.py
    19 0
    19 5
    19 3
    19 6
    19 8
    19 2
    19 1
    19 7
    19 14
    19 4
    19 9
    19 12
    19 10
    19 15
    19 13
    19 11
    19 16
    19 19
    19 17
    19 18
    
    Process finished with exit code 0

    会发现,原本应该是相对应的,结果全部都混乱了,都对应19了。这就是线程混乱。怎么解决呢,继续往下看

    import time
    import threading
    from threading import local
    
    class Foo(local):
        pass
    
    foo = Foo()
    
    def add(i):
        foo.num = i
        time.sleep(1)
        print(foo.num,i)
    
    for i in range(20):
        th = threading.Thread(target=add,args=(i,))
        th.start()

    结果:

    D:python3.6.6python3.exe E:/python/Flasktest/flask1/f1.py
    2 2
    0 0
    4 4
    3 3
    1 1
    6 6
    7 7
    10 10
    9 9
    11 11
    12 12
    14 14
    5 5
    18 18
    8 8
    13 13
    16 16
    17 17
    15 15
    19 19
    
    Process finished with exit code 0

    这回就显示正常了。这就是local所做的事情

  • 相关阅读:
    Tree
    a letter and a number
    A problem is easy
    connect设置超时的方法
    C++客户端访问Java服务端发布的SOAP模式的WebService接口
    gSoap的“error LNK2001: 无法解析的外部符号 _namespaces”解决方法
    先序序列和后序序列并不能唯一确定二叉树
    二叉树的非递归遍历
    web service,soap ,http,tcp,udp
    byte[]数组和int之间的转换
  • 原文地址:https://www.cnblogs.com/zhangjunkang/p/10251168.html
Copyright © 2011-2022 走看看