import queue
import datetime
# threading 这个方法 不仅有 加锁 和 释放锁 两个功能
# threading.Condition 在threading基础上 增加了 wait()功能
import threading
import functools
# 测试方法
def test(id):
print("3秒以后删除用户%s的缓存" % id)
# 延时任务类
class DelayTask:
# 具体时间和任务
def __init__(self, time, job):
# 实例化 时间用秒计算 变成公共的
self.time = time
self.job = job
# 将时间转换成秒
def Seconds(dt):
# 参数dt = 期待时间 - 当前时间 转换为秒
# 一天24个小时一小时60分一分60秒
return dt.seconds + dt.days * 24 * 60 * 60
# 延时队列
class Delay_Queue(queue.PriorityQueue):
def __init__(self):
self.can_done = threading.Condition()
# super关键字 既能用自己的方法 也能继承父类的方法 俩个方法不会被覆盖
super(Delay_Queue, self).__init__()
# 入队 参数传入 延时时间和方法
def put_team(self, team):
# 传入元祖 (延时时间,方法)
self.put((team.time, team))
# 获取对顶数据
def _peek(self):
# 获取锁
self.not_empty.acquire()
# _qsize返回队列长度
try:
# 遍历队列 如果队列不是空
Delay_Queue--延时队列操作详解哦
最新推荐文章于 2024-06-25 11:29:32 发布
本文详细介绍了使用Python的Tornado框架实现延时队列的操作,涵盖了核心概念、实现原理及具体代码示例,帮助读者理解如何在Tornado中构建和应用延时队列。
摘要由CSDN通过智能技术生成