DAY12 thread pool, RABBITMQ, and SQLAlchemy

Source: Internet
Author: User
Tags redis rabbitmq

1, with implementation of context management
#! /usr/bin/env python
#-*-Coding:utf-8-*-
# Author:wanghuafeng


#with实现上下文管理
Import Contextlib
@contextlib. ContextManager
def worker_state (self, State_list, worker_thread):
"""
Used to record the number of threads waiting in a thread
:p Aram Self:
:p Aram State_list:
:p Aram Worker_thread:
: return:
"""
#第二步
State_list.append (Worker_thread)
Try
#第三步
Yield
Finally
#执行第五步
State_list.remove (Worker_thread)

Free_list = []
Current_thread = "Wang"
#第一步执行with, call the Worker_state method
With Worker_state (Free_list, Current_thread):
#第四步
Print (123)
Print (456)
#执行完成出去的瞬间
2. With implementation Socket context
#! /usr/bin/env python
#-*-Coding:utf-8-*-
# Author:wanghuafeng


Import Contextlib
Import socket

@contextlib. ContextManager
def context_socket (host, Port):
SK = Socket.socket ()
Sk.bind ((host, Port))
Sk.listen (5)
Try
Yield SK
Finally
Sk.close ()

With Context_socket (' 127.0.0.1 ', 8888) as sock:
Print (sock)
3. Redis Connection Pool
Import Redis
Pool = Redis. ConnectionPool (host= ' 192.168.195.128 ', port=6379)
R = Redis. Redis (Connection_pool=pool)
Pipe = R.pipeline (transaction=true)
R.set (' name ', ' Wanghuafeng ')
R.set (' role ', ' haha ')
t = Pipe.execute ()
Print (R.get (' role '))
4. Redis Publish, Subscribe
Import Redis

Class Redishelper:
def __init__ (self):
Self.__conn = Redis. Redis (host= ' 192.168.195.128 ')
self.chan_sub = ' FM98.8 '
self.chan_pub = ' FM98.8 '

def public (self, msg):
Self.__conn.publish (Self.chan_pub, msg)
Return True

def subscribe (self):
Pub = Self.__conn.pubsub ()
Pub.subscribe (Self.chan_sub)
Pub.parse_response ()
Return pub
#! /usr/bin/env python
#-*-Coding:utf-8-*-
# Author:wanghuafeng


From MONITOR.S22 import Redishelper

obj = Redishelper ()
Redis_sub = Obj.subscribe ()

While True:
msg = Redis_sub.parse_response ()
Print (msg)
#! /usr/bin/env python
#-*-Coding:utf-8-*-
# Author:wanghuafeng


From MONITOR.S22 import Redishelper

obj = Redishelper ()
Obj.public (' helo ')
5, RabbitMQ
#! /usr/bin/env python
#-*-Coding:utf-8-*-
# Author:wanghuafeng

Import Pika

# ######################### producer #########################

Connection = Pika. Blockingconnection (Pika. Connectionparameters (
host= ' localhost '))
Channel = Connection.channel ()

Channel.queue_declare (queue= ' Hello ')

Channel.basic_publish (exchange= ",
routing_key= ' Hello ',
body= ' Hello world! ')
Print ("[x] Sent ' Hello world! '")
Connection.close ()
#! /usr/bin/env python
#-*-Coding:utf-8-*-
# Author:wanghuafeng


Import Pika

# ########################## Consumer ##########################

Connection = Pika. Blockingconnection (Pika. Connectionparameters (
host= ' localhost '))
Channel = Connection.channel ()

Channel.queue_declare (queue= ' Hello ')


DEF callback (ch, method, properties, body):
Print ("[x] Received%r"% body)


Channel.basic_consume (Callback,
Queue= ' Hello ',
No_ack=true)

Print (' [*] waiting for messages. To exit Press CTRL + C ')
Channel.start_consuming ()

DAY12 thread pool, RABBITMQ, and SQLAlchemy

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.