分享一篇關於Python實現環形佇列高效定時器

delete1111111111111發表於2020-12-17

定時器Python實現程式碼

import time
import redis
import multiprocessing


class Base:

    """
    redis配置
    """
    redis_conf = {}

    """
    環形佇列使用redis進行儲存
    """
    _ri = None

    """
    定時器輪盤大小
    """
    slot_num = 15

    """
    儲存環形佇列使用的redis快取key
    """
    cache_key = 'wheel:slot_'

    def __init__(self, **kwargs):
        for k in kwargs:
            if hasattr(self, k):
                setattr(self, k, kwargs[k])

        self._ri = redis.Redis(**self.redis_conf)


class Timer(Base):
    """
    當前slot的下標
    """
    _current = 0

    """
    事件處理
    """
    event_handler = None

    def worker(self):
        """
        # TODO 測試每個卡槽有1W事件ID的處理效率
        獨立程式,分發事件id給事件處理器
        :return:
        """
        key = self.cache_key + str(self._current)

        # 獲取當前卡槽中需要觸發的事件ID
        event_ids = self._ri.zrangebyscore(key, 0, 0)

        # 刪除當前卡槽中需要觸發的事件ID
        self._ri.zremrangebyscore(key, 0, 0)

        # 把當前卡槽剩下的事件ID全部遍歷出來,減少一次剩餘迴圈次數
        surplus_event_ids = self._ri.zrange(key, 0, -1)

        for mid in surplus_event_ids:
            self._ri.zincrby(key, mid, -1)

        # 把事件ID轉交給handler處理
        for mid in event_ids:
            self.event_handler(eid=mid)

        exit(0)

    def run(self):
        """
        啟動程式
        :return:
        """
        while True:
            p = multiprocessing.Process(target=self.worker)
            p.start()

            time.sleep(1)

            self._current = int(time.time()) % self.slot_num


class TimerEvent(Base):

    def add(self, event_id, emit_time):
        """
        新增事件ID到定時器
        :param event_id: 事件ID
        :param emit_time: 觸發時間
        :return:
        """
        current_time = int(time.time())
        diff = emit_time - current_time

        if diff > 0:
            # 計算迴圈次數
            cycle = int(diff / self.slot_num)
            # 計算要存入的slot的索引
            index = (diff % self.slot_num + current_time % self.slot_num) % self.slot_num

            res = self._ri.zadd(self.cache_key + str(index), str(event_id), cycle)
            return True if res else False

        return False

    # TODO 批量新增同一時間,不同事件ID

    # TODO 批量新增不同時間,不同事件ID

通過環形佇列實現高效任務觸發的設計說明

https://github.com/meygd/ood/issues/1
https://github.com/meygd/ood/issues/2
https://github.com/meygd/ood/issues/3
https://github.com/meygd/ood/issues/4
https://github.com/meygd/ood/issues/5
https://github.com/meygd/ood/issues/6
https://github.com/meygd/ood/issues/7
https://github.com/meygd/ood/issues/8
https://github.com/meygd/ood/issues/9
https://github.com/meygd/ood/issues/10
https://github.com/meygd/ood/issues/11
https://github.com/meygd/ood/issues/12
https://github.com/meygd/ood/issues/13
https://github.com/meygd/ood/issues/14
https://github.com/meygd/ood/issues/15
https://github.com/meygd/ood/issues/16
https://github.com/meygd/ood/issues/17
https://github.com/meygd/ood/issues/18
https://github.com/meygd/ood/issues/19
https://github.com/meygd/ood/issues/20
https://github.com/meygd/ood/issues/21
https://github.com/meygd/ood/issues/22
https://github.com/meygd/ood/issues/23
https://github.com/meygd/ood/issues/24
https://github.com/meygd/ood/issues/25
https://github.com/meygd/ood/issues/26
https://github.com/meygd/ood/issues/27
https://github.com/meygd/ood/issues/28
https://github.com/meygd/ood/issues/29
https://github.com/meygd/ood/issues/30
https://github.com/meygd/ood/issues/31
https://github.com/meygd/ood/issues/32
https://github.com/meygd/ood/issues/33
https://github.com/meygd/ood/issues/34
https://github.com/meygd/ood/issues/35
https://github.com/meygd/ood/issues/36
https://github.com/meygd/ood/issues/37
https://github.com/meygd/ood/issues/38
https://github.com/meygd/ood/issues/39
https://github.com/meygd/ood/issues/40
https://github.com/meygd/ood/issues/41
https://github.com/meygd/ood/issues/42
https://github.com/meygd/ood/issues/43
https://github.com/meygd/ood/issues/44
https://github.com/meygd/ood/issues/45
https://github.com/meygd/ood/issues/46
https://github.com/meygd/ood/issues/47
https://github.com/meygd/ood/issues/48
https://github.com/meygd/ood/issues/49
https://github.com/meygd/ood/issues/50
https://github.com/meygd/ood/issues/51
https://github.com/meygd/ood/issues/52
https://github.com/meygd/ood/issues/53
https://github.com/meygd/ood/issues/54
https://github.com/meygd/ood/issues/55
https://github.com/meygd/ood/issues/56
https://github.com/meygd/ood/issues/57
https://github.com/meygd/ood/issues/58
https://github.com/meygd/ood/issues/59
https://github.com/meygd/ood/issues/60
https://github.com/meygd/ood/issues/61
https://github.com/meygd/ood/issues/62
https://github.com/meygd/ood/issues/63
https://github.com/meygd/ood/issues/64
https://github.com/meygd/ood/issues/65
https://github.com/meygd/ood/issues/66
https://github.com/meygd/ood/issues/67
https://github.com/meygd/ood/issues/68
https://github.com/meygd/ood/issues/69
https://github.com/meygd/ood/issues/70
https://github.com/meygd/ood/issues/71
https://github.com/meygd/ood/issues/72
https://github.com/meygd/ood/issues/73
https://github.com/meygd/ood/issues/74
https://github.com/meygd/ood/issues/75
https://github.com/meygd/ood/issues/76
https://github.com/meygd/ood/issues/77
https://github.com/meygd/ood/issues/78
https://github.com/meygd/ood/issues/79
https://github.com/meygd/ood/issues/80
https://github.com/meygd/ood/issues/81
https://github.com/meygd/ood/issues/82
https://github.com/meygd/ood/issues/83
https://github.com/meygd/ood/issues/84
https://github.com/meygd/ood/issues/85
https://github.com/meygd/ood/issues/86
https://github.com/meygd/ood/issues/87
https://github.com/meygd/ood/issues/88
https://github.com/meygd/ood/issues/89
https://github.com/meygd/ood/issues/90
https://github.com/meygd/ood/issues/91
https://github.com/meygd/ood/issues/92
https://github.com/meygd/ood/issues/93
https://github.com/meygd/ood/issues/94
https://github.com/meygd/ood/issues/95
https://github.com/meygd/ood/issues/96
https://github.com/meygd/ood/issues/97
https://github.com/meygd/ood/issues/98
https://github.com/meygd/ood/issues/99
https://github.com/meygd/ood/issues/100
https://github.com/meygd/ood/issues/101
https://github.com/meygd/ood/issues/102
https://github.com/meygd/ood/issues/103
https://github.com/meygd/ood/issues/104
https://github.com/meygd/ood/issues/105
https://github.com/meygd/ood/issues/106
https://github.com/meygd/ood/issues/107
https://github.com/meygd/ood/issues/108
https://github.com/meygd/ood/issues/109
https://github.com/meygd/ood/issues/110
https://github.com/meygd/ood/issues/111
https://github.com/meygd/ood/issues/112
https://github.com/meygd/ood/issues/113
https://github.com/meygd/ood/issues/114
https://github.com/meygd/ood/issues/115
https://github.com/meygd/ood/issues/116
https://github.com/meygd/ood/issues/117
https://github.com/meygd/ood/issues/118
https://github.com/meygd/ood/issues/119
https://github.com/meygd/ood/issues/120
https://github.com/meygd/ood/issues/121
https://github.com/meygd/ood/issues/122
https://github.com/meygd/ood/issues/123
https://github.com/meygd/ood/issues/124
https://github.com/meygd/ood/issues/125
https://github.com/meygd/ood/issues/126
https://github.com/meygd/ood/issues/127
https://github.com/meygd/ood/issues/128
https://github.com/meygd/ood/issues/129
https://github.com/meygd/ood/issues/130
https://github.com/meygd/ood/issues/131
https://github.com/meygd/ood/issues/132
https://github.com/meygd/ood/issues/133
https://github.com/meygd/ood/issues/134
https://github.com/meygd/ood/issues/135
https://github.com/meygd/ood/issues/136
https://github.com/meygd/ood/issues/137
https://github.com/meygd/ood/issues/138
https://github.com/meygd/ood/issues/139
https://github.com/meygd/ood/issues/140
https://github.com/meygd/ood/issues/141
https://github.com/meygd/ood/issues/142
https://github.com/meygd/ood/issues/143
https://github.com/meygd/ood/issues/144
https://github.com/meygd/ood/issues/145
https://github.com/meygd/ood/issues/146
https://github.com/meygd/ood/issues/147
https://github.com/meygd/ood/issues/148
https://github.com/meygd/ood/issues/149
https://github.com/meygd/ood/issues/150
https://github.com/meygd/ood/issues/151
https://github.com/meygd/ood/issues/152
https://github.com/meygd/ood/issues/153
https://github.com/meygd/ood/issues/154
https://github.com/meygd/ood/issues/155
https://github.com/meygd/ood/issues/156
https://github.com/meygd/ood/issues/157
https://github.com/meygd/ood/issues/158
https://github.com/meygd/ood/issues/159
https://github.com/meygd/ood/issues/160
https://github.com/meygd/ood/issues/161
https://github.com/meygd/ood/issues/162
https://github.com/meygd/ood/issues/163
https://github.com/meygd/ood/issues/164
https://github.com/meygd/ood/issues/165
https://github.com/meygd/ood/issues/166
https://github.com/meygd/ood/issues/167
https://github.com/meygd/ood/issues/168
https://github.com/meygd/ood/issues/169
https://github.com/meygd/ood/issues/170
https://github.com/meygd/ood/issues/171
https://github.com/meygd/ood/issues/172
https://github.com/meygd/ood/issues/173
https://github.com/meygd/ood/issues/174
https://github.com/meygd/ood/issues/175
https://github.com/meygd/ood/issues/176
https://github.com/meygd/ood/issues/177
https://github.com/meygd/ood/issues/178
https://github.com/meygd/ood/issues/179
https://github.com/meygd/ood/issues/180
https://github.com/meygd/ood/issues/181
https://github.com/meygd/ood/issues/182
https://github.com/meygd/ood/issues/183
https://github.com/meygd/ood/issues/184
https://github.com/meygd/ood/issues/185
https://github.com/meygd/ood/issues/186
https://github.com/meygd/ood/issues/187
https://github.com/meygd/ood/issues/188
https://github.com/meygd/ood/issues/189
https://github.com/meygd/ood/issues/190
https://github.com/meygd/ood/issues/191
https://github.com/meygd/ood/issues/192
https://github.com/meygd/ood/issues/193
https://github.com/meygd/ood/issues/194
https://github.com/meygd/ood/issues/195
https://github.com/meygd/ood/issues/196
https://github.com/meygd/ood/issues/197
https://github.com/meygd/ood/issues/198
https://github.com/meygd/ood/issues/199
https://github.com/meygd/ood/issues/200
https://github.com/meygd/ood/issues/201
https://github.com/meygd/ood/issues/202
https://github.com/meygd/ood/issues/203
https://github.com/meygd/ood/issues/204
https://github.com/meygd/ood/issues/205
https://github.com/meygd/ood/issues/206
https://github.com/meygd/ood/issues/207
https://github.com/meygd/ood/issues/208
https://github.com/meygd/ood/issues/209
https://github.com/meygd/ood/issues/210
https://github.com/meygd/ood/issues/211
https://github.com/meygd/ood/issues/212
https://github.com/meygd/ood/issues/213
https://github.com/meygd/ood/issues/214
https://github.com/meygd/ood/issues/215
https://github.com/meygd/ood/issues/216
https://github.com/meygd/ood/issues/217
https://github.com/meygd/ood/issues/218
https://github.com/meygd/ood/issues/219
https://github.com/meygd/ood/issues/220
https://github.com/meygd/ood/issues/221
https://github.com/meygd/ood/issues/222
https://github.com/meygd/ood/issues/223
https://github.com/meygd/ood/issues/224
https://github.com/meygd/ood/issues/225
https://github.com/meygd/ood/issues/226
https://github.com/meygd/ood/issues/227
https://github.com/meygd/ood/issues/228
https://github.com/meygd/ood/issues/229
https://github.com/meygd/ood/issues/230
https://github.com/meygd/ood/issues/231
https://github.com/meygd/ood/issues/232
https://github.com/meygd/ood/issues/233
https://github.com/meygd/ood/issues/234
https://github.com/meygd/ood/issues/235
https://github.com/meygd/ood/issues/236
https://github.com/meygd/ood/issues/237
https://github.com/meygd/ood/issues/238
https://github.com/meygd/ood/issues/239
https://github.com/meygd/ood/issues/240
https://github.com/meygd/ood/issues/241
https://github.com/meygd/ood/issues/242
https://github.com/meygd/ood/issues/243
https://github.com/meygd/ood/issues/244
https://github.com/meygd/ood/issues/245
https://github.com/meygd/ood/issues/246
https://github.com/meygd/ood/issues/247
https://github.com/meygd/ood/issues/248
https://github.com/meygd/ood/issues/249
https://github.com/meygd/ood/issues/250
https://github.com/meygd/ood/issues/251
https://github.com/meygd/ood/issues/252
https://github.com/meygd/ood/issues/253
https://github.com/meygd/ood/issues/254
https://github.com/meygd/ood/issues/255
https://github.com/meygd/ood/issues/256
https://github.com/meygd/ood/issues/257
https://github.com/meygd/ood/issues/258
https://github.com/meygd/ood/issues/259
https://github.com/meygd/ood/issues/260
https://github.com/meygd/ood/issues/261
https://github.com/meygd/ood/issues/262
https://github.com/meygd/ood/issues/263
https://github.com/meygd/ood/issues/264
https://github.com/meygd/ood/issues/265
https://github.com/meygd/ood/issues/266
https://github.com/meygd/ood/issues/267
https://github.com/meygd/ood/issues/268
https://github.com/meygd/ood/issues/269
https://github.com/meygd/ood/issues/270
https://github.com/meygd/ood/issues/271
https://github.com/meygd/ood/issues/272
https://github.com/meygd/ood/issues/273
https://github.com/meygd/ood/issues/274
https://github.com/meygd/ood/issues/275
https://github.com/meygd/ood/issues/276
https://github.com/meygd/ood/issues/277
https://github.com/meygd/ood/issues/278
https://github.com/meygd/ood/issues/279
https://github.com/meygd/ood/issues/280
https://github.com/meygd/ood/issues/281
https://github.com/meygd/ood/issues/282
https://github.com/meygd/ood/issues/283
https://github.com/meygd/ood/issues/284
https://github.com/meygd/ood/issues/285
https://github.com/meygd/ood/issues/286
https://github.com/meygd/ood/issues/287
https://github.com/meygd/ood/issues/288

  1. redis集合【slot】
  • 以redis多個有規律的鍵名的有序集合組成環形陣列
key_1
key_2
....
key_n
  • 有序集合

命令

ZADD key score member
有序集合中包含兩部分, 一個是score, 一個是member

score作為剩餘迴圈次數  
meber作為事件ID

  1. python多程式
  • 計算當前時間應該處理的卡槽

    當前slot索引 = (當前時間 % 卡槽總數 + 當前時間戳 % 卡槽總數) % 卡槽總數

"%"為取餘數操作
  • 建立獨立子程式處理

    當前子程式需要快速讀取的剩餘迴圈次數為0事件ID

    刪除當前slot已取出的事件ID

    開始把事件ID依次轉交給事件handler處理

應用說明

  1. 啟動定時器
import Timer
import time


def event_handler(eid):
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), eid)


t = Timer(redis_conf={
    'host': '127.0.0.1',
    'port': 6379,
    'password': '123456',
    'db': 0
}, event_handler=event_handler)

times = int(time.time())

print('Current Time is ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(times)))

t.run()
  1. 新增需要延時觸發事件ID
import TimerEvent
import time


te = TimerEvent(redis_conf={
    'host': '127.0.0.1',
    'port': 6379,
    'password': '123456',
    'db': 0
})

times = int(time.time())

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(times)))

after_seconds_alert = 20

for x in range(100):
    te.add(x, times + after_seconds_alert + x)

print('Firs Emit will happened at ' + time.strftime(
    'Start:%Y-%m-%d %H:%M:%S',
    time.localtime(times + after_seconds_alert))
)
參考文章

相關文章