python第十一天

先写博客,复习学习到的知识点后再写作业,这次作业也比较简单哈,这几周的课程不是没有作业就是作业很简单,北方表示很喜欢,学习进度一下子就上去了,Python持续学习中,运维方面下一阶段转战docker~~~openstack的各个组件已经完全理解,网络模式理解到了80%左右,ceph进度较慢,仅仅理解了估计不到40%,马上10.1了放假再补上吧,也祝大家国庆节快乐,北方一直以为国庆只放假3天的,没想到是7天

10.1学习计划如下:学习openstack剩余知识点,python进度进展到最少第13天,把docker技术入门及实践书籍看完,离10.1结束还有11天,加油

 

线程:用于IO密集形

进程:用于CPU密集形

协程:用于IO密集形

 

1.线程创建有2种方式,一种自己创建类实现顶掉python自带的创建线程的类去实现,一种使用python自带的创建线程的类。使用的话当然使用人家直接提供的了

1 import threading

 

2.队列,先进先出,也有先进后出的,这个特性不就是栈么....还有两头都可以出线程的类是threading的,wtf,什么鬼,反正标准的就是先进先出就是

 1 #!/usr/bin/env python
 2 import queue
 3 q = queue.Queue(2)
 4 print(q.empty())            #验证队列是否为空,返回True或者False
 5 q.put(1)                    #向对列中放置一个消息,可以是任何类型
 6 q.put(2)                    #在放一个
 7 print(q.qsize())            #检测当前队列中任务的个数
 8 print(q.get())              #获取一个任务,遵循先进先出的规律,因此出来的是1
 9 print(q.empty())
10 q.put(3, block=True, timeout=2)     #当队列中的任务已经到达队列可容纳上限后之后的任务就会等待
11                                     # 可以设置超时时间,或者选择不等待他
12 print(q.qsize())
1 C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C:/Users/Administrator/PycharmProjects/ACE/study11/s2.py
2 True
3 2
4 1
5 False
6 2
7 
8 Process finished with exit code 0

生产者消费者模型,只要是解决高并发请求的,也就是我们架构中部署的消息队列服务器,例如rabbitmq,zeromq等等,消息队列服务器的好处有很多了,最主要的功能就是消峰跟解耦,这个知识点在开发这里就不对介绍了

 1 #!/usr/bin/env python
 2 import queue
 3 import threading
 4 import time
 5 q = queue.Queue()
 6 
 7 
 8 def productor(arg):
 9     q.put(str(arg) + '--食物')
10 
11 for i in range(300):
12     t = threading.Thread(target=productor, args=(i, ))
13     t.start()
14 
15 
16 def cousumer(arg):
17     while True:
18         print(arg, q.get())
19         time.sleep(0.1)
20 
21 for j in range(3):
22     t = threading.Thread(target=cousumer, args=(j, ))
23     t.start()

线程同时对数据进行操作,那么就会产生脏数据,脏数据是什么鬼,好比买衣服仅剩一件了,但是有10个人同时买了,这时候库存变为了-9,这明显不合理,这就产生了脏数据,因此我们需要加一把锁给线程,线程锁有以下几种:互斥锁,信号量,事件,定时器,条件

 1 #!/usr/bin/env python
 2 import threading
 3 import time
 4 
 5 NUM = 10
 6 
 7 
 8 def func(lock):
 9     global NUM
10     lock.acquire()          #上锁,acquire()固定写法
11     NUM -= 1
12     time.sleep(1)
13     print(NUM)
14     lock.release()          #开锁,release()固定写法
15 
16 # lock = threading.BoundedSemaphore(5)      #信号量,可每次释放指定个数的线程
17 lock = threading.RLock()                    #互斥锁,每次只会释放一个线程,可复用,包含了threading.Lock()功能
18 lock = threading.Lock()                     #互斥锁,每次只会释放一个线程,不可复用,一般不用
19 for i in range(30):
20     t = threading.Thread(target=func, args=(lock,))
21     t.start()
1 #!/usr/bin/env python
2 from threading import Timer         #定时器,线程相关的都在threading类内
3 
4 
5 def hello():
6     print('hello world')
7 
8 t = Timer(1, hello)                 #1秒后执行hello函数
9 t.start()
 1 #!/usr/bin/env python
 2 import threading
 3 
 4 
 5 def func(i, con):
 6     print(i)
 7     con.acquire()
 8     con.wait()
 9     print(i + 100)
10     con.release()
11 
12 c = threading.Condition()       #条件
13 for i in range(10):
14     t = threading.Thread(target=func, args=(i, c,))
15     t.start()
16 
17 while True:
18     inp = input('>>>')
19     if inp == 'q':
20         break
21     else:
22         c.acquire()             #固定写法
23         c.notify(int(inp))      #每次放行几个线程
24         c.release()             #固定写法
 1 #!/usr/bin/env python
 2 import threading
 3 
 4 
 5 def func(i, e):
 6     print(i)
 7     e.wait()                    #检测此时锁的状态
 8     print(i + 100)
 9 
10 event = threading.Event()       #事件
11 for i in range(10):
12     t = threading.Thread(target=func, args=(i, event))
13     t.start()
14 
15 event.clear()                   #线程禁止通行,默认状态
16 inp = input('>>>')
17 if inp == '1':
18     event.set()                 #线程放行
 1 C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C:/Users/Administrator/PycharmProjects/ACE/study11/s5.py
 2 0
 3 1
 4 2
 5 3
 6 4
 7 5
 8 6
 9 7
10 8
11 9
12 >>>1
13 100
14 103
15 104
16 107
17 108
18 101
19 105
20 109
21 102
22 106
23 
24 Process finished with exit code 0

 进程相关:与线程所具有的功能类似,所有功能都在multiprocessing类内,类内直接提供了进程池(线程池线程类未提供,需要自己定义)

 1 #!/usr/bin/env python
 2 from multiprocessing import Pool            #进程池
 3 import time
 4 
 5 
 6 def f1(arg):
 7     time.sleep(1)
 8     print(arg)
 9 
10 if __name__ == '__main__':
11     pool = Pool(5)                          #存放指定个数的进程
12     for i in range(30):
13         # pool.apply(func=f1, args=(i,))      #每次只释放一个进程
14         pool.apply_async(func=f1, args=(i,))       #多个进程同时启用
15     time.sleep(2)
16     pool.terminate()                        #立即终止
17     # pool.close()                          #等所有任务结束后执行
18     pool.join()                             

协程相关:首先要下载gevent模块,在cmd中执行pip install gevent即可,这是一条线程持续执行的过程,效率较高,遇到IO会自动切换

 1 #!/usr/bin/env python
 2 from gevent import monkey; monkey.patch_all()
 3 import gevent
 4 import requests
 5 
 6 
 7 def f(url):
 8     print('get %s' % url)
 9     resp = requests.get(url)
10     data = resp.text
11     print('%d bytes from %s' % (len(data), url))
12 
13 gevent.joinall([
14     gevent.spawn(f, 'http://www.baidu.com'),
15     gevent.spawn(f, 'http://www.163.com'),
16     gevent.spawn(f, 'http://www.xy2.com'),
17 ])
1 C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C:/Users/Administrator/PycharmProjects/ACE/study11/s11.py
2 get http://www.baidu.com
3 get http://www.163.com
4 get http://www.xy2.com
5 2381 bytes from http://www.baidu.com
6 95564 bytes from http://www.xy2.com
7 721371 bytes from http://www.163.com
8 
9 Process finished with exit code 0
posted @ 2016-09-27 10:40  北方姆Q  阅读(330)  评论(0编辑  收藏  举报