模式匹配之Boyer-Moore算法
python操作Redis之连接池
一. python操作Redis之连接池
redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池
# 连接池
# 把他做成单例,写在一个文件里面,import它
import redis # 拿到一个redis的连接池
Pool = redis.ConnectionPool(host='127.0.0.1', port=6379, max_connections=10) # 从池子中拿一个链接
conn = redis.Redis(connection_pool=pool,decode_responses=True)
print(conn.get('name').decode('utf-8'))
# 如果想要并发操作,就需要写成单列,以模块导入就是一个单例,把他做成单例,写在一个文件里面,import它,就是一个单例
Python中设置Redis连接超时时间
当配置出现问题,或者redis服务挂了的时候,那么对redis的操作就会一直没有响应,那么可以通过设置redis的连接池的连接超时时间参数socket_connect_timeout,
再通过ping()方法来检查Redis的连接有效性。
# 这里把连接时间设置为1秒
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0, socket_connect_timeout=1)
rs = redis.Redis(connection_pool=pool)
try:
re.ping()
except TimeoutError:
pring('redis connection timeout')
使用阻塞连接池
前面介绍了Redis客户端可以通过连接池方式初始化,在redis-py中提供了多种连接池以满足实际需求,这里介绍使用阻塞连接池(BlockingConnectionPool)操作Redis。
阻塞连接池的特点是:当连接池中没有空闲的连接时,会等待timeout秒,直到获取到连接或超时报错。
import redis
import threading
class RedaisExexThread(threading.Thread):
def __init__(self, redis_client):
threading.Thread.__init__(self)
self.redis_client = redis_client
def run(self):
res = self.redis_client.get('foo')
print(res)
def main():
pool = redis.BlockingConnectionPool(host='localhost', port=6379, db=0, max_connections=2, timeout=5)
redis_client = redis.Redis(connection_pool=pool)
thread1 = RedaisExexThread(redis_client)
thread2 = RedaisExexThread(redis_client)
thread3 = RedaisExexThread(redis_client)
thread4 = RedaisExexThread(redis_client)
thread5 = RedaisExexThread(redis_client)
thread6 = RedaisExexThread(redis_client)
thread7 = RedaisExexThread(redis_client)
thread8 = RedaisExexThread(redis_client)
thread9 = RedaisExexThread(redis_client)
thread10 = RedaisExexThread(redis_client)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()
thread10.start()
if __name__ == '__main__':
main()
这里创建了一个阻塞连接池,其容量为2,超时等待时间为5秒。用10个线程并发的方式测试阻塞连接池,可以正常使用。感兴趣的可以用普通连接池测试下,看会出现什么问题。
另外根据文档描述,这个阻塞连接池可以给多个客户端使用,实际测试也是可以的。感兴趣可以自己试下。
redis-py的使用很简单,只需要用redis.Redis建立连接,即可对Redis进行操作。例如:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
r.get('foo')
默认情况下,Python3返回bytes,Python2返回str。
多线程使用
redis-py是线程安全的Redis客户端,可以放心的在多线程代码中使用。
import redis
import threading
class RedaisExexThread(threading.Thread):
def __init__(self, redis_client):
threading.Thread.__init__(self)
self.redis_client = redis_client
def run(self):
res = self.redis_client.get('foo')
print(res)
def main():
redis_client = redis.Redis(host='localhost', port=6379, db=0)
thread1 = RedaisExexThread(redis_client)
thread2 = RedaisExexThread(redis_client)
thread3 = RedaisExexThread(redis_client)
thread4 = RedaisExexThread(redis_client)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
if __name__ == '__main__':
main()
python redis-py模块使用详解
模式匹配之Boyer-Moore算法的更多相关文章
- Boyer Moore算法(字符串匹配)
上一篇文章,我介绍了KMP算法. 但是,它并不是效率最高的算法,实际采用并不多.各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Mo ...
- Boyer–Moore (BM)字符串搜索算法
在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法.它由Bob Boyer和J Strother Moore设计于1977年.此算法仅对搜索目标字符串(关键字)进行预处 ...
- 第32讲:List的基本操作实战与基于模式匹配的List排序算法实现
今天来学习一下list的基本操作及基于模式匹配的排序操作 让我们从代码出发 val bigData = List("hadoop","spark") val d ...
- 软件设计师_朴素模式匹配算法和KMP算法
1.从主字符串中匹配模式字符串(暴力匹配) 2. KMP算法
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- Boyer-Moore 字符串匹配算法
字符串匹配问题的形式定义: 文本(Text)是一个长度为 n 的数组 T[1..n]: 模式(Pattern)是一个长度为 m 且 m≤n 的数组 P[1..m]: T 和 P 中的元素都属于有限的字 ...
- DPI (Deep Packet Inspection) 深度包检测技术
详解DPI与网络回溯分析技术 随着网络通讯技术进步与发展,网络通讯已跨入大数据时代,如何监控各类业务系统的通讯数据在大数据流量中传输质量,以及针对海量的网络通讯数据的范畴中存在少量的恶意流量的检测,避 ...
- 【Java字符序列】Pattern
简介 Pattern,正则表达式的编译表示,操作字符序列的利器. 整个Pattern是一个树形结构(对应于表达式中的‘|’),一般为链表结构,树(链表)的基本元素是Node结点,Node有各种各样的子 ...
- DPI深度报文检测架构及关键技术实现
DPI深度报文检测架构及关键技术实现 当前DPI(Deep Packet Inspect深度报文识别)技术是安全领域的关键技术点之一,围绕DPI技术衍生出的安全产品类型也非常的多样.在分析DPI的进一 ...
- 2019-8-31-C#-对-byte-数组进行模式搜索
title author date CreateTime categories C# 对 byte 数组进行模式搜索 lindexi 2019-08-31 16:55:58 +0800 2018-07 ...
随机推荐
- [转]Github 简明教程
如果你是一枚Coder,但是你不知道Github,那么我觉的你就不是一个菜鸟级别的Coder,因为你压根不是真正Coder,你只是一个Code搬运工. 但是你如果已经在读这篇文章了,我觉的你已经知道G ...
- 并发测试 JavaDemo
https://github.com/oldratlee/fucking-java-concurrency /** * @author Jerry Lee */ public class Testee ...
- Centos 7搭建Gitlab服务器超详细(转)
一. 安装并配置必要的依赖关系 在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知) ,wget,以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问. 1.安装s ...
- Android新特性--ConstraintLayout完全解析
Android新特性--ConstraintLayout完全解析 本篇文章的主题是ConstraintLayout.其实ConstraintLayout是Android Studio 2.2中主要的新 ...
- python 取整的两种方法
问题简介: 要把一个浮点数(float)整数部分提取出来.比如把“2.1”变成“2”的这一过程:现在我们给这个过程起一个名字叫“取整”.那么它 在python中大致可以有两种写法 写法1)类型转换: ...
- php分享十六:php读取大文件总结
一:file函数读取 file()函数的效率很底下 如果是有规律的文件.比如每行一条相应数据.那么尽量不要是用file()函数,可以使用file_get_contents()然后用explode切割. ...
- C语言浮点数存储结构
float类型占四个字节,每个字节占8位,总共32位,其内存结构如下图: 31位为符号位:0表示正数,1表示负数 31~23位:共8位表示指数位,内存存储数据从0~2^8-1=255,由于指数可以是正 ...
- sql server中调用c#写的dll里的方法
最近有一项目: 一超市管理系统单机版,运行在WIN2003+SQL2005上,每天超市关门都都会关电脑,现客户要新加功能,每天关门下班后回家可以上网查看超市管理系统的数据库里的相关数据,然后再做一些原 ...
- Mac OS X 10.8.5 安装编译glib
过程比较坎坷,就此记录一番一遍后人 1. 下载pkg-config然后解压安装: ./configure make -j 24 sudo make install 2.下载libiconv解压安装 . ...
- 【Unity】11.3 基本碰撞体(箱体、球形、胶囊、网格)
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 碰撞组件(Collider) 是另一种必须随刚体 (Rigidbody) 添加的组件,以便允许它和其他组件发生碰撞.或者 ...