In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article is about Python using redis message queues. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.
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 as the number of mappings added print(redis.hget ('price ', ' cake')) #Get the value with the key cakeand 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 Usage Example
import redis
import 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 sequentially, 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 no longer receives 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)
About python use redis message queue method to share here, hope the above content can have some help to everyone, can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.