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
thought
V2EX  ›  Python

请教关于 flask 用 gunicorn 的时候重复写入数据库的问题

  •  
  •   thought · 2015-06-10 12:35:40 +08:00 · 2629 次点击
    这是一个创建于 3246 天前的主题,其中的信息可能已经有所发展或是发生改变。
    @app.route('/update')
    def update():
        for x in range(4):
            import writedatabase as h
            b=h.pushit(1,1,1)
        return redirect(url_for('home'))
    

    以上代码,理论上是写入数据库4条数据,但是实际操作中会写入8条之多。
    gunicorn 设置是 -w 4
    另外用python自带的运行程序没有问题,感觉和多进程有关。
    希望大神帮忙看看如何解决。
    另外请教通过网址更新数据库,或者每天自动更新数据库,有没有什么好的方法?

    3 条回复    2015-11-04 14:38:14 +08:00
    kkzxak47
        1
    kkzxak47  
       2015-06-13 21:31:15 +08:00
    reloader=True?
    之前遇到的问题是用APScheduler时,每一个任务会启动两次,后来发现是因为reloader设为True了,flask实际会启动两个进程。
    thought
        2
    thought  
    OP
       2015-06-13 22:03:06 +08:00
    @kkzxak47 已经解决了,是浏览器多次请求造成的。
    laoyuan
        3
    laoyuan  
       2015-11-04 14:38:14 +08:00
    Get 要幂等 Get 要幂等 Get 要幂等
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3097 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.