RABBITMQ Queue
Installing http://www.rabbitmq.com/install-standalone-mac.html
Installing the Python RabbitMQ module
1234567 |
pip install pika or easy_install pika or 源码 https: / / pypi.python.org / pypi / pika |
To achieve the simplest queue communication
Produce
1 ImportPika2Connection = Pika. Blockingconnection (Pika. Connectionparameters ("192.168.244.130", 15672))3Channel =Connection.channel ()4 #declaring a queue5Channel.queue_declare (queue='Hello')6Channel.basic_publish (exchange="",7routing_key='Hello',8BODY ='Hello world!')9 Print("[x] Sent ' Hello world!")TenConnection.close ()
View Code
Consume
1 ImportPika2 3Connection = Pika. Blockingconnection (Pika. Connectionparameters ("192.168.16.23"))4Channel =Connection.channel ()5Channel.queue_declare (queue="Holle", durable=True)6 defCallback (ch,method,properties,body):7 Print(ch,method,properties)8 Print("[x] Received%r"%body)9Ch.basic_ack (delivery_tag=Method.delivery_tag)TenChannel.basic_qos (prefetch_count=1) One Channel.basic_consume (Callback, AQueue="Holle", -no_ack=True) - the - Print('[*] waiting for messages. To exit press CTRL + C') -Channel.start_consuming ()
View Code
Redis
Redis is a key-value storage System . Similar to memcached, it supports storing more value types, including string (string), list ( linked list ), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.
First, Redis installation and basic use
1234 |
wget http: / / download.redis.io / releases / Redis - 3.0 6.tar .gz tar xzf redis - 3.0 6.tar .gz cd Redis - 3.0 6 make |
Start the service side
Start the client
12345 |
src / redis - cli redis> set foo bar OK redis> get foo "bar" |
Second, Python operation Redis
1234567 |
sudo pip install redis or sudo easy_install redis or 源码安装 详见:https: / / github.com / WoLpH / redis - py |
1. Operation mode
Redis-py provides two classes of Redis and Strictredis for implementing Redis commands, Strictredis is used to implement most of the official commands, and using official syntax and commands, Redis is a subclass of Strictredis, Used for backwards compatibility with older versions of Redis-py.
12345678 |
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
redis
r
=
redis.Redis(host
=
‘10.211.55.4‘
, port
=
6379
)
r.
set
(
‘foo‘
,
‘Bar‘
)
print
r.get(
‘foo‘
)
|
2. Connection Pool
Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool.
12345678910 |
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
redis
pool
=
redis.ConnectionPool(host
=
‘10.211.55.4‘
, port
=
6379
)
r
= redis.Redis(connection_pool
=
pool)
r.
set
(
‘foo‘
,
‘Bar‘
)
print
r.get(
‘foo‘
)
|
3. Operation
String operation, a string in Redis is stored in memory according to a name corresponding to a value.
Set (name, value, Ex=none, Px=none, Nx=false, Xx=false)
Set name Alex sets the value in Redis, default, does not exist then creates, exists then modifies parameters: Ex, Expiration Time (seconds) px, Expiration Time (msec) NX, if set to True, only if name does not exist , the current set operation only executes XX, and if set to true, the pre-post set operation is executed only if name exists
SETNX (name, value) setnx AAA Juck Set value, perform set action (add) only if name does not exist
Set (name, value, time) set CCC CCC ex 30# set Value # parameter: # Time_ms, Expiration time (numeric milliseconds or Timedelta object
Mset (*args, **kwargs) batch setting values such as: Mset (k1= ' v1 ', k2= ' v2 ') or mget ({' K1 ': ' v1 ', ' K2 ': ' V2 '})
Get (name) get value
Mget (keys, *args) bulk get such as: mget (' k ', ' K2 ') or r.mget ([' K ', ' K2 '])
Getset (name, value) getset K2 K2 Set a new value and get the original value
Python rabbitmq queue/redis