推荐学习书目
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
chaleaochexist
V2EX  ›  Python

延迟任务的增加和删除

  •  
  •   chaleaochexist · Jun 18, 2019 · 2675 views
    This topic created in 2544 days ago, the information mentioned may be changed or developed.
    两个 rest api
    1. 增加一个延迟任务
    2. 删除某延迟任务

    延迟的时间很大,可能 2 周.2 个月都有可能.

    这样的需求用啥做比较好?

    apscheduler 的问题是 起多个进程处理起来比较麻烦.
    celery 可以处理类似问题吗?


    谢谢了
    8 replies    2019-06-25 23:59:32 +08:00
    chaleaochexist
        1
    chaleaochexist  
    OP
       Jun 18, 2019
    任务是一次性的,不是定时任务.
    chaleaochexist
        2
    chaleaochexist  
    OP
       Jun 18, 2019
    距离成功只差最后一步.

    django 中如何使用 apscheduler?
    只启动一次,可以在多个 view 中使用.
    并且在启动的时候,可以使用 django.cache.lock
    Vegetable
        3
    Vegetable  
       Jun 19, 2019   ❤️ 1
    感觉方向不是很恰当,celery 可以实现,直接使用 task.delay 或者 send_task 配合 countdown 参数就可以直接延迟执行,刚好也是执行一次.
    但是 2 周好久啊,内存已经不靠谱了,celery+redis 勉强可以算作是可靠的吧

    如果我来做我选择数据库+crontab
    leishi1313
        4
    leishi1313  
       Jun 19, 2019 via Android
    赞同#3,延迟那么久用什么异步没意义了,还是持久化到数据库然后跑定时任务比较好
    chaleaochexist
        5
    chaleaochexist  
    OP
       Jun 19, 2019
    @Vegetable
    @leishi1313
    任务丢出去,如何取消?

    这不是定时任务,是一次性延时任务.并且可以被取消.你的意思是说,定时任务跑起来,然后删掉?
    tt67wq
        6
    tt67wq  
       Jun 19, 2019
    老老实实丢在数据库里面,再加个定时任务来消费,数据库的事务性能免去一大堆用其他乱七八糟 queue 带来的麻烦
    leishi1313
        7
    leishi1313  
       Jun 19, 2019 via Android
    @chaleaochexist 任务开始跑了不用再取消吧,没跑不就还在数据库嘛,删掉或者标记一下就好了呀。我们说的定时任务是一种服务,你可以设定每隔多久跑一次,然后你就可以设置比如每分钟数据库查一下有没有到期的任务,有就扔给 worker 跑就好了。celery 也有类似功能,搜“ celery cron ”
    sujin190
        8
    sujin190  
       Jun 25, 2019
    https://github.com/snower/forsun

    使用 redis 保存的话,开 redis 持久化就不会丢失,添加删除都很方便
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2820 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 01:23 · PVG 09:23 · LAX 18:23 · JFK 21:23
    ♥ Do have faith in what you're doing.