国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 云林县| 正镶白旗| 临沧市| 衡东县| 云霄县| 巧家县| 措勤县| 卓尼县| 博白县| 龙海市| 六枝特区| 丰城市| 泸水县| 闵行区| 临武县| 军事| 临西县| 东辽县| 双鸭山市| 崇仁县| 武穴市| 台州市| 泽州县| 台南市| 彝良县| 翼城县| 梁河县| 安远县| 邯郸县| 绵阳市| 泗洪县| 安顺市| 呼伦贝尔市| 和平县| 手机| 治多县| 西乡县| 平陆县| 四平市| 鄂托克前旗| 高台县|