推荐学习书目
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 · Jun 10, 2015 · 3120 views
    This topic created in 3989 days ago, the information mentioned may be changed or developed.
    @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 replies    2015-11-04 14:38:14 +08:00
    kkzxak47
        1
    kkzxak47  
       Jun 13, 2015
    reloader=True?
    之前遇到的问题是用APScheduler时,每一个任务会启动两次,后来发现是因为reloader设为True了,flask实际会启动两个进程。
    thought
        2
    thought  
    OP
       Jun 13, 2015
    @kkzxak47 已经解决了,是浏览器多次请求造成的。
    laoyuan
        3
    laoyuan  
       Nov 4, 2015
    Get 要幂等 Get 要幂等 Get 要幂等
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1073 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 18:21 · PVG 02:21 · LAX 11:21 · JFK 14:21
    ♥ Do have faith in what you're doing.