Python rabbitmq queue/redis

Source: Internet
Author: User
Tags rabbitmq install redis redis server

RABBITMQ Queue  

Installing http://www.rabbitmq.com/install-standalone-mac.html

Installing the Python RabbitMQ module

1234567 pip install pikaoreasy_install pikaor源码 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

1 src/redis-server

Start the client

12345 src/redis-cliredis> setfoo barOKredis> get foo"bar"

Second, Python operation Redis

1234567 sudo pip install redisorsudo easy_install redisor源码安装详见: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 -*-importredis=redis.Redis(host=‘10.211.55.4‘, port=6379)r.set(‘foo‘‘Bar‘)printr.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 -*-importredispool =redis.ConnectionPool(host=‘10.211.55.4‘, port=6379)= redis.Redis(connection_pool=pool)r.set(‘foo‘‘Bar‘)printr.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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.