V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zqyisasd
V2EX  ›  Python

django 的 view 中如何阻塞请求

  •  
  •   zqyisasd · 2018-09-13 10:15:45 +08:00 · 1695 次点击
    这是一个创建于 2023 天前的主题,其中的信息可能已经有所发展或是发生改变。
    具体如图中,一个是验证码,一个验证码颜色,https://gitee.com/zhuqiyu/django_monitor/blob/master/index_img/index.png
    目前是让验证码颜色函数阻塞 0.01 秒,但是有时候验证码函数超过 0.01 秒就会导致第二张图片加载不出来
    代码地址 https://gitee.com/zhuqiyu/django_monitor/blob/master/monitor/ad/views.py (auth 是验证码函数,auth2 是验证码颜色函数)
    代码
    auth_code_color = None
    def auth(request):
    """ 验证码函数 ,authCode 类来自 Authcode.py
    @:return
    auth_code_img, 二进制图片
    """
    auth_code = authCode()
    auth_code_img = auth_code.gene_code()
    auth_code_text = auth_code.text
    global auth_code_color
    auth_code_color = auth_code.create_color()
    request.session["verify_code"] = auth_code_text
    return HttpResponse(auth_code_img, 'image/png')
    def auth2(request):
    time.sleep(0.01)
    global auth_code_color
    return HttpResponse(auth_code_color, 'image/png')
    zqyisasd
        1
    zqyisasd  
    OP
       2018-09-13 10:50:14 +08:00
    请问能用什么方式将 time.sleep(0.01)替换了吗,或者思路也行。之前在 auth 函数中添加一个变量,值为 1,在 auth2 中-1,可能实现的有问题,结果比现在还差。
    zqyisasd
        2
    zqyisasd  
    OP
       2018-09-13 10:56:31 +08:00
    目前最容易出现的是在服务重启之后的前两次访问,后面就一般不会复现。
    sujin190
        3
    sujin190  
       2018-09-13 11:21:56 +08:00
    为什么不在第一个请求结束后再发起第二个请求啊
    zqyisasd
        4
    zqyisasd  
    OP
       2018-09-13 11:25:24 +08:00
    因为我不会前端。。
    zqyisasd
        5
    zqyisasd  
    OP
       2018-09-13 11:25:30 +08:00
    <div style="display: inline-block;"><img name="img_code" src="/ad/auth/?0.42844671870369355" onclick="this.src='/ad/auth/?'+Math.random();document.getElementById('verify_code_color').src='/ad/auth2/?'+Math.random()"/></div>
    <div style="position: absolute;left: 90px;top: 10px;">输入 <img id="verify_code_color" src="/ad/auth2/?0.42844671870369355"/> 验证码</div>
    xpresslink
        6
    xpresslink  
       2018-09-13 11:38:13 +08:00
    别瞎折腾了百度一下 django-captcha
    你自己写的那个 login 也是乱七八糟的,还不直接 django 自己带的 auth 模块呢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1367 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:34 · PVG 07:34 · LAX 16:34 · JFK 19:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.