#!/usr/bin/env python
# coding=utf- import time
#import redis class RedisLock(object):
def __init__(self, key):
#self.rdcon = redis.Redis(host='', port=, password="", db=)
self._lock =
self.lock_key = "%s_dynamic_test" % key @staticmethod
def get_lock(cls,timeout=):
print("get_lock!")
'''
while cls._lock != :
timestamp = time.time() + timeout +
cls._lock = cls.rdcon.setnx(cls.lock_key, timestamp)
if cls._lock == or (time.time() > cls.rdcon.get(cls.lock_key) and time.time() > cls.rdcon.getset(cls.lock_key, timestamp)):
print("get lock")
break
else:
time.sleep(0.3)
''' @staticmethod
def release(cls):
print("release!")
'''
if time.time() < cls.rdcon.get(cls.lock_key):
print("release lock")
cls.rdcon.delete(cls.lock_key)
''' def deco(cls):
def _deco(func):
def __deco(*args, **kwargs):
print("before {} called [{}].".format(func.__name__, cls))
cls.get_lock(cls)
try:
return func(*args, **kwargs)
finally:
cls.release(cls)
return __deco
return _deco @deco(RedisLock(""))
def myfunc(i):
print(i)
print("myfunc() called.")
time.sleep(10) if __name__ == "__main__":
myfunc()

输出

before myfunc called [<__main__.RedisLock object at 0x0000026239D0A7F0>].
get_lock! myfunc() called.
release!

python 装饰器,传递类以及参数的更多相关文章

  1. python 装饰器修改调整函数参数

    简单记录一下利用python装饰器来调整函数的方法.现在有个需求:参数line范围为1-16,要求把9-16的范围转化为1-8,即9对应1,10对应2,...,16对应8. 下面是例子: def fo ...

  2. Python装饰器实现类Java注解功能

    最近想用Python写一个简单生成器,类似指定类型和范围,返回指定列表: 比如想要  0 ~ 3 的整数,则 我只需要指定: 最小:0, 最大:3, 步长:1 则返回一个 [0,1,2,3] 的列表 ...

  3. python 装饰器 传递参数简单案例

    def debug(func): def wrapper(*args, **kwargs): # 指定宇宙无敌参数 print "[DEBUG]: enter {}()".form ...

  4. Python装饰器AOP 不定长参数 鸭子类型 重载(三)

    1 可变长参数与关键字参数 *args代表任意长度可变参数 **kwargs代表关键字参数 用*args和**kwargs只是为了方便并没有强制使用它们. 缺省参数即是调用该函数时,缺省参数的值若未被 ...

  5. python 装饰器 对类和函数的装饰

    #装饰器:对类或者函数进行功能的扩展  很多需要缩进的没有进行缩进'''#第一步:基本函数def laxi(): print('拉屎')#调用函数laxi()laxi() print('======= ...

  6. python装饰器同时支持有参数和无参数的练习题

    ''' 预备知识: …… @decorator def f(*args,**kwargs): pass # 此处@decorator  等价于 f = decorator(f) @decorator2 ...

  7. python 装饰器 (多个参数的函数,带参数的装饰器)

    最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...

  8. python——装饰器(不定长参数,闭包,装饰器)示例

    def func(functionName): print("正在装饰") def func_in(*args, **kargs): print("------func_ ...

  9. Python装饰器详解

    python中的装饰器是一个用得非常多的东西,我们可以把一些特定的方法.通用的方法写成一个个装饰器,这就为调用这些方法提供一个非常大的便利,如此提高我们代码的可读性以及简洁性,以及可扩展性. 在学习p ...

  10. Python装饰器的高级用法(翻译)

    原文地址 https://www.codementor.io/python/tutorial/advanced-use-python-decorators-class-function 介绍 我写这篇 ...

随机推荐

  1. oracle练手(一)

    练手001 1.列出至少有一个员工的所有部门 select dname from dept where deptno in (select deptno from emp); select dname ...

  2. ScrumBasic开发记录

    ScrumBasic 是基于asp.net core 1.0的开源敏捷管理软件.目前第一版.目前只有很基础的东西.希望我能将这个项目演变下去. 地址:https://github.com/CAH-Fl ...

  3. DPDK latencystats库使用方案

    初始化 注意务必调用 rte_metrics_init /* init latency stats */ /* @TODO should we remove this in product env? ...

  4. gperftools源码分析和项目应用 - CPU Profiler

    gperftools源码分析和项目应用 - CPU Profiler 原文:https://blog.csdn.net/yubo112002/article/details/81076821 原文链接 ...

  5. python现状

    自从官方宣布 2020 年 1 月后不再更新维护 Python2,已经有一大批开源软件将其抛弃.今天,抛弃 Python2 的名单上又多了一个重磅软件.Python2 是 Python 官方在 200 ...

  6. Javascript简单教程汇总

    什么是函数 一段定义好的代码,并可以反复使用的代码块 函数的作用 提升代码的可复用性,将一段代码进行预定义,需要使用的时候才触发 代码块 形成了一个相对独立的作用域 语法: function  函数名 ...

  7. docker第二篇 Docker基础用法

    Docker中的容器 lxc -> libcontainer -> runC OCI (Open Container Initiative) 由Linux基金会主导于2015年6月创立 作 ...

  8. MySQL间隙锁问题

    间隙锁(Gap Lock):锁加在不存在的空闲空间,可以是两个索引记录之间,也可能是第一个索引记录之前或最后一个索引之后的空间. 最近用户反馈说系统老是出现insert时,等待超时了,最后发现是ins ...

  9. JPA的API介绍、工具类抽取

    1.Persistence对象 Persistence对象主要作用是用于获取EntityManagerFactory对象的 .通过调用该类的createEntityManagerFactory静态方法 ...

  10. list 列表 函数的引用

    方法 意义 L.index(v [, begin[, end]]) 返回对应元素的索引下标, begin为开始索引,end为结束索引,当 value 不存在时触发ValueError错误 L.inse ...