国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python單線程實(shí)現(xiàn)多個(gè)定時(shí)器示例

2020-02-23 05:15:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

單線程實(shí)現(xiàn)多個(gè)定時(shí)器

NewTimer.py
代碼如下:
#!/usr/bin/env python

from heapq import *
from threading import Timer
import threading
import uuid
import time
import datetime
import sys
import math

global TimerStamp
global TimerTimes

class CancelFail(Exception):
    pass

class Slot(object):
    def __init__(self, period=0, interval=1, function=None, args=[], kwargs={}):
        self.period = period
        self.pc = 0
        self.interval = interval
        self.fire = 0
        self.id = uuid.uuid1()
        self.function = function
        self.args = args
        self.kwargs = kwargs

#system resolution millisecond        
class NewTimer(object):

    #set enough time make thread sleep, when NewTimer empty set enoug time, too
    #make sure sum of your timer call back function execute time shorter than resolution
    #todo use a worker thread to operate timer call back function
    def __init__(self, resolution=1000):
        global TimerStamp
        TimerStamp = int(time.time() * 1000)

        self.nofire = sys.maxint #next fire time interval
        self.firestamp = self.nofire + TimerStamp
        self.resolution = resolution# 1s

        self.lock = threading.RLock()

        self.wait = dict()
        self.ready = dict()

        self._start()

    """ private operate ready list """
    def _addToReadyList(self, slot, firestamp):
        box = dict( [ (slot.id, slot)])
        if not self.ready.has_key( firestamp ):
            self.ready.update( [(firestamp, box)] )
        else:
            boxs = self.ready.get(firestamp)
            boxs.update( box )

    def _delFromReadyList(self, slot):
        boxs = self.ready.get(slot.fire)
        try:
            box = boxs.pop(slot.id)

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 澄迈县| 玛多县| 鱼台县| 肥乡县| 图们市| 澄江县| 贡嘎县| 买车| 和平区| 麦盖提县| 龙门县| 河北省| 田东县| 万安县| 房山区| 贵港市| 阿巴嘎旗| 龙海市| 大石桥市| 濮阳市| 房山区| 都匀市| 邢台县| 天台县| 桐城市| 星子县| 富源县| 浮梁县| 英吉沙县| 和平区| 东丰县| 木里| 云和县| 宁蒗| 海门市| 邹城市| 锡林郭勒盟| 洱源县| 宾川县| 民权县| 甘谷县|