pythonに基づいて操作redisとメッセージキューを実現します。


操作レポート

import redis
redisPool = redis.ConnectionPool(host='192.168.100.50', port=6379, db=8)
redis= redis.Redis(connection_pool=redisPool)

redis.set('key','values')
redis.get('com')
redis.append('keys','values')
redis.delete('keys')

print(redis.getset('name','Mike')) #  name Mike       value
print(redis.mget(['name','age']))  #  name  age  value
print(redis.setnx('newname','james')) #       ,   
print(redis.mset({'name1':'smith','name2':'curry'})) #    
print(redis.msetnx({'name3':'ltf','name4':'lsq'}))  #        
print(redis.incr('age',1))  #age   value  1
print(redis.decr('age',5))  #age   value  5
print(redis.append('name4','is a sb'))  # name4 value    is a sb        
print(redis.substr('name',1,4))  #    name

print(redis.sadd('tags','Book','Tea','Coffee')) #       3
print(redis.srem('tags','Book')) #         
print(redis.spop('tags'))  #          
print(redis.smove('tags','tags1','Coffee'))
print(redis.scard('tags')) #   tags       
print(redis.sismember('tags', 'Book')) #   Book   tags    
print(redis.sinter('tags', 'tags1')) #     tags   tags1   
print(redis.sunion('tags', 'tags1')) #     tags   tags1   
print(redis.sdiff('tags', 'tags1')) #     tags   tags1   
print(redis.smembers('tags')) #     tags     

print(redis.hset('price','cake',5)) #     price          ,  1         
print(redis.hsetnx('price','book',6)) #     price          ,  1         
print(redis.hget('price', 'cake')) #      cake     5
print(redis.hmset('price',{'banana':2,'apple':3,'pear':6,'orange':7}))  #      
print(redis.hmget('price', ['apple', 'orange'])) #   apple orange      b'3',b'7'
print(redis.hincrby('price','apple',3))  #apple   3  6
print(redis.hexists('price', 'banana')) #  price banana       True
print(redis.hdel('price','banana'))  # price   banana   1
print(redis.hlen('price')) #   price   
print(redis.hkeys('price')) #          
print(redis.hvals('price')) #          
print(redis.hgetall('price')) #          

print(redis.rpush('list',1,2,3)) #    list       1,2,3     
print(redis.lpush('list',0))  #    list       0     
print(redis.llen('list'))  #       
print(redis.lrange('list',1,3)) #       1      3          
print(redis.lindex('list',1))  #     1   -value
print(redis.lset('list',1,5)) # list      1      5
print(redis.lpop('list')) #  list     
print(redis.rpop('list'))  #  list      
print(redis.blpop('list'))  #  list     
print(redis.brpop('list'))  #        
print(redis.rpoplpush('list','list1'))  #  list          list1   
メッセージキューの使用例

import redis
import json
redisPool = redis.ConnectionPool(host='192.168.100.50', port=6379, db=8)
client = redis.Redis(connection_pool=redisPool)

#          redis  ,sort            
while True:
  num = 0
  for i in range(0, 100):
    num = num + 1
    # params info
    params_dict = {"name": f"test {num}", "sort":num}

    client.rpush("test", json.dumps(params_dict))

  #         
  result = client.lrange("test", 0, 100)
  print(result)
  import time
  time.sleep(10)

import redis
import time
import multiprocessing
import time
import os
import random

redisPool = redis.ConnectionPool(host='192.168.100.50', port=6379, db=8)
client = redis.Redis(connection_pool=redisPool)

def test1(msg):
  t_start = time.time()
  print("%s    ,    %d" % (msg, os.getpid()))
  time.sleep(random.random() * 2)
  t_stop = time.time()
  print("%s    ,  %.2f" % (msg, t_stop - t_start))

while True:
  number = client.llen('test')
  print("            ", number)
  p = 100
  if number > p-1:
    print("-----start-----")
    a = []
    for i in range(p):
      result = client.lpop("test")
      a.append(result)
    print(" 10     ", a)
    po = multiprocessing.Pool(p)
    for i in range(0, p):
      # Pool().apply_async(      ,(          ,))
      #                     
      po.apply_async(test1, (a[i],))
    po.close() #      ,   po        
    po.join() #   po          ,    close    
    print("-----end-----")
    time.sleep(2)
  elif number < p and number > 0:
    print("-----start-----")
    a = []
    for i in range(number):
      a = []
      result = client.lpop("test")
      a.append(result)
    print("  10        ", a)
    po = multiprocessing.Pool(number)
    for i in a:
      # Pool().apply_async(      ,(          ,))
      #                     
      po.apply_async(test1, (a,))

    po.close() #      ,   po        
    po.join() #   po          ,    close    
    print("-----end-----")
    time.sleep(2)
  elif number == 0:
    print("        ")
    time.sleep(2)
  else:
    time.sleep(2)
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。