解决python-redis-lock分布式锁的问题

作者:Loganer 时间:2023-05-23 18:57:49 

python-redis-lock

官方文档
不错的博文可参考

问题背景

在使用celery执行我们的异步任务时,为了提高效率,celery可以开启多个进程来启动对应的worker。
但是会出现这么一种情况:在获取到数据源之后要对数据库进行扫描,根据UUID来断定是插入还是更新,两个worker 同时 (相差0.001S)拿到了UUID但是在其中一个没插入时,另一个也扫描完了数据库,这时这两个worker都会认为自己拿到的UUID是在数据库中没有存在过的,所以都会调用INSERT方法来进行插入操作。

几种解决方案

为了解决这个问题,一般有如下解决方案.
分布式锁家族:

数据库:

  • 排它锁(悲观锁)

  • 乐观锁

Redis

  1. 自己实现Redis SET SETNX 操作,结合Lua脚本确保原子操作

  2. RedLock Redis里分布式锁实现的算法,争议比较大,谨慎使用

  3. python-redis-lock 本文将要介绍的技术。这个库提供的分布式锁很灵活,是否需要超时?是否需要自动刷新?是否要阻塞?都是可选的。没有最好的算法,只有最合适的算法,开发人员应该根据实际需求场景谨慎选择具体用哪一种技术去实现。

设计思路:

解决python-redis-lock分布式锁的问题 

Zookeeper
这个应该是功能最强大的,比较专业,稳定性好。我还没使用过,日后玩明白了再写篇文章总结一下。

扩展思路

在celery的场景下也可以使用celery_once进行任务去重操作, celery_once底层也是使用redis进行实现的。
可以参考这篇

Talk is cheap, show me your code!

一个简单的demo


import random
import time
import threading
import redis_lock
import redis

HOST = 'YOUR IP LOCATE'
PORT = '6379'
PASSWORD = 'password'

def get_redis():
   pool = redis.ConnectionPool(host=HOST, port=PORT, password=PASSWORD, decode_responses=True, db=2)
   r = redis.Redis(connection_pool=pool)
   return r

def ask_lock(uuid):
   lock = redis_lock.Lock(get_redis(), uuid)
   if lock.acquire(blocking=False):
       print(" %s Got the lock." % uuid)
       time.sleep(5)
       lock.release()
       print(" %s Release the lock." % uuid)
   else:
       print(" %s Someone else has the lock." % uuid)

def simulate():
  for i in range(10):
       id = random.randint(0, 5)
       t = threading.Thread(target=ask_lock, args=(str(id)))
       t.start()

simulate()

Output:

 4 Got the lock.
 5 Got the lock.
 3 Got the lock.
 5 Someone else has the lock.
 5 Someone else has the lock.
 2 Got the lock.
 5 Someone else has the lock.
 4 Someone else has the lock.
 3 Someone else has the lock.
 3 Someone else has the lock.
 2 Release the lock.
 5 Release the lock.
 4 Release the lock.
 3 Release the lock.

来源:https://blog.csdn.net/wyh1618/article/details/120973410

标签:python,redis,布式锁
0
投稿

猜你喜欢

  • 实用又漂亮的BootstrapValidator表单验证插件

    2024-05-09 10:39:52
  • python函数形参用法实例分析

    2023-09-08 21:07:09
  • SQL函数将某个字段合并在一起的操作

    2024-01-23 10:11:07
  • 如何在mac环境中用python处理protobuf

    2021-02-26 08:42:06
  • Dreamweaver小技巧:超高速下载图像

    2009-07-14 21:59:00
  • 深入理解Python中的元类(metaclass)

    2021-02-25 22:10:00
  • JavaScript实现计算器的四则运算功能

    2024-04-16 10:36:16
  • python使用socket远程连接错误处理方法

    2023-05-13 04:56:48
  • 3个JS控制图片滚动的效果

    2007-10-23 13:40:00
  • win2003安装sqlserver 2000提示无法验证产品密钥的解决方法

    2024-01-27 00:18:39
  • Golang与python线程详解及简单实例

    2023-03-13 18:55:25
  • Python实现爬取网页中动态加载的数据

    2021-08-11 18:35:27
  • python实现一个围棋小游戏

    2022-02-02 18:52:50
  • Python实现PS滤镜的万花筒效果示例

    2023-11-15 10:17:25
  • 如何使用Typora+MinIO+Python代码打造舒适协作环境

    2023-11-12 15:12:10
  • python3+opencv 使用灰度直方图来判断图片的亮暗操作

    2024-01-01 07:07:23
  • 浅谈pycharm下找不到sqlalchemy的问题

    2022-06-17 14:51:15
  • python的类方法和静态方法

    2021-04-14 21:05:22
  • 深入探寻javascript定时器

    2024-04-29 13:36:55
  • 为什么不建议在go项目中使用init()

    2024-05-11 09:17:47
  • asp之家 网络编程 m.aspxhome.com