Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example of using redis + message queue for python operation

Shulou Source: shulou.com Published: 2022-06-02 09:39:18 10月03日 Update

Operation redisimport 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')) #assign name to Mike and return previous valueprint(redis.mget ('name ',' age')) #Output valueprint(redis.setnx ('newname ','james')) for name and age keys #Assign print(redis.mset ('name1':'smith',' name2 ':'curry'}) if key does not exist #Batch assign print(redis.msetnx ('name3':'ltf','name4':'lsq'})) #batch assignment print(redis.incr ('age ', 1)) only if there is no #age value plus 1print(redis.decr ('age ', 5)) #age value minus 5print(redis.append ('name 4','is a sb')) #append is a sb to the value of name4 to return the string length print(redis.substr ('name ',1,4)) #intercept key nameprint(redis.sadd ('tags ',' Book','Tea',' Coffee')) #return collection length 3print(redis.srem ('tags ',' Book')) #return number of deleted data print(redis. pop ('tags ')) #Randomly delete and return the element print (redis.smove('tags','tags1','Coffee'))print (redis.scard ('tags ')) #Get the number of elements in the tags collection print (redis.sismember ('tags ', ' Book')) #Determine if Book is in the set of tags print (redis.sinter ('tags ', ' tags1 ')) #Returns the intersection of set tags and set tags1 print (redis.sunion ('tags ', ' tags1 ')) #Returns the print union of set tags and set tags1 (redis.sdiff ('tags ', ' tags1')) #Returns the difference print of set tags and set tags1 (redis.smembers ('tags ')) #Returns all elements of the set tags print (redis.hset ('price ',' cake', 5)) #Add a mapping to the hash table with the key price, return 1 as the number of mappings added print (redis.hsetnx ('price ',' book', 6)) #Add a mapping to the hash table with the key price and return 1, i.e. the number of mappings added print(redis.hget ('price ', ' cake')) #Get the value with the key cake and return 5print(redis.hmset ('price ',{'banana':2,'apple':3,'pear':6,'orange':7})) #Batch add mapping print(redis.hmget ('price ', [' apple','orange']) #Query apple and orange values output b' 3', b'7'print(redis.hincrby ('price ',' apple', 3)) #apple map plus 3 for 6print(redis.hexists ('price ', ' banana')) #banana exists in price returns Trueprint(redis.hdel ('price ',' banana')) #Remove banana from price and return 1print (redis.hlen ('price ')) #print the length of price (redis.hkeys ('price ')) #print all mapping keys (redis.hvals ('price ')) #print all map keys (redis.hgetall ('price ')) #print all mapped key pairs print(redis.rpush ('list', 1,2,3)) #add 1,2,3 to the end of a list with key name list return length print(redis.lpush ('list ', 0)) #Add 0 to the head of a list with key name list Return Length print(redis.llen ('list ')) #Returns the length of the list print(redis.lrange ('list ', 1,3)) #Returns the list of index ranges starting at index 1 and ending at index 3 print(redis.lindex ('list', 1)) #Return elements with index 1-valueprint(redis.lset ('list ', 1,5)) #Reassign list elements with index 1 to 5print(redis.lpop ('list')) #Delete first element of list print(redis.rpop ('list ')) #Delete last element of list print(redis.blpop ('list ')) #delete the first element of list print(redis.brpop ('list ')) #delete last element print(redis.rpoplpush ('list ','list 1')) #Remove the tail element of list and add it to the head of list1 Message Queue Use example import redisimport jsonredisPool = redis.ConnectionPool(host='192.168.100.50', port=6379, db=8)client = redis.Redis(connection_pool=redisPool)#Insert five pieces of data into redis queue in sequence, sort parameter is used to verify the order of ejection 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)) #View destination queue data result = client.lrange("test", 0, 100) print(result) import time time.sleep(10)import redisimport timeimport multiprocessingimport timeimport osimport randomredisPool = 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 started execution with process number %d" % (msg, os.getpid())) time.sleep(random.random() * 2) t_stop = time.time() print("%s execution complete, took %.2f" % (msg, t_stop - t_start))while True: number = client.llen('test') print("The current queue task number is", number) p = 100 if number > p-1: print("-----start-----") a = [] for i in range(p): result = client.lpop("test") a.append(result) print("Read every 10", a) po = multiprocessing.Pool(p) for i in range(0, p): # Pool().apply_async(target to be invoked,(argument ancestor passed to target,)) #Each loop will invoke the target with a child process that is free po.apply_async(test1, (a[i],)) po.close() #Closes the process pool, after which po will no longer receive new requests po.join() #Wait for all child processes in po to complete execution, which must be placed after the close statement 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("Read less than 10 items once", a) po = multiprocessing.Pool(number) for i in a: # Pool().apply_async(target to be invoked,(argument ancestor passed to target,)) #Each loop will invoke the target with a child process that is free po.apply_async(test1, (a,)) po.close() #Closes the process pool, after which po will no longer receive new requests po.join() #Wait for all child processes in po to complete execution, which must be placed after the close statement print("-----end-----") time.sleep(2) elif number == 0: print("No tasks to process") time.sleep(2) else: time.sleep(2)

Tags: Element target process length output index queue number parameter data task header idle statement order meta-ancestor loop example message intersection Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Shulou Information Docker Apple