新聞中心
Delay tasks have become increasingly important for distributed system, such as order expiration, timed task, etc. Traditional solutions using cron solutions can achieve scheduling, but it has certn limitations, such as the lack of flexibility, the non-real-time experience, and the lack of scalability. Fortunately, with the emergence of Redis technology, We can use Redis to easily implement delayed job processing.

寶山網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、自適應網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)。
Redis is an open source, high performance distributed memory database, which can store k-v structures, and support data persistence (with AOF and RDB). Therefore, we can use Redis as a message queue to delay tasks.
The principle of Redis delaying task is to save tasks in Redis database with a specific score, and expiration time. When the score expired, Redis will return all tasks with expired scores, and then our job can be executed.
The specific solutions are as follows:
1. Add a delayed task list:
“`py
conn.zadd(REDIS_KEY, {COMMAND_NAME + ‘_’ + NOW_TIME: NOW_TIME}
2. Set up a scheduled task of 1 minute to get the task that has expired:
```py
def get_expired_task():
valid_time = time.time()
expired_task = conn.zrangebyscore(REDIS_KEY, 0, valid_time)
for item in expired_task:
job_name = item.rsplit('_', 1)[0]
# Execute the job here
conn.zrem(REDIS_KEY, item)
3. Add corresponding scheduled task to cron according to the granularity of the task:
“`sh
* * * * * python get_expired_task.py
Redis is not only efficient and flexible, but also supports distributed clustering, which is suitable for large scale delayed tasks. With the support of cron tasks and python access Redis, we can easily implement timed task scheduling and delay job processing.
香港云服務器機房,創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)云服務器廠商,回大陸優(yōu)化帶寬,安全/穩(wěn)定/低延遲.創(chuàng)新互聯(lián)助力企業(yè)出海業(yè)務,提供一站式解決方案。香港服務器-免備案低延遲-雙向CN2+BGP極速互訪!
本文名稱:利用Redis簡化延遲任務處理(使用redis做延遲處理)
文章起源:http://fisionsoft.com.cn/article/cojieip.html


咨詢
建站咨詢
