在redis上建立的分布式信号量和互斥量。

Redis-Semaphore的Python项目详细描述


https://travis-ci.org/bluele/redis-semaphore.svg?branch=master

在redis上建立的分布式信号量和互斥量。

安装

要安装redis信号灯,只需:

pip install redis-semaphore

或者,您可以下载存储库并通过执行以下操作手动安装:

git clone git@github.com:bluele/redis-semaphore.git
cd redis-semaphore
python setup.py install

示例

from redis import Redis
from redis_semaphore import Semaphore
from threading import Thread
import urllib2
import time

semaphore = Semaphore(Redis(), count=2, namespace='example')


def task(i):
    url = 'https://www.google.co.jp/'
    with semaphore:
        print('id: {} => {}'.format(i, urllib2.urlopen(url).code))
        print('sleep...')
        time.sleep(2)


def main():
    threads = list()
    for i in range(5):
        threads.append(Thread(target=task, args=(i,)))
    for th in threads:
        th.start()
    for th in threads:
        th.join()

if __name__ == '__main__':
    main()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
多次调用预期方法的java捕获参数(EasyMock)   java我试图得到某个学生的平均值,但它将所有值相加   使用Java更新文件中所有出现的字符串   java从依赖包导入Spring配置(*.xml)   在Java中,如何从不同的偏移量读取大块的字符串文件?   java 安卓 studio未在windows 8中运行   java getResourceAsStream()不读取任何内容   java Google Collections 1.0是否已经专业化?   模型视图控制器ASP。NET母版页在Java中的等效性   计时器TimerTask类在java中只能运行一次   多级继承和foreach Java循环   excel csv到xsl java,有一列带有某种货币(如$400)   java在来电时多次更改BlackBerry的callIncoming()   java通过JNLP启动JavaFX2.0应用程序会引发异常