1. 程式人生 > >python學習——day9(ssh,線程和進程,信號量,隊列,生產者消費者模型) Alex地址:http://www.cnblogs.com/alex3714/articles/5230609.html

python學習——day9(ssh,線程和進程,信號量,隊列,生產者消費者模型) Alex地址:http://www.cnblogs.com/alex3714/articles/5230609.html

png 接口 count() day bound 共享 car 共享內存 top

一、python上模擬ssh

1.ssh,ssh_ftp

pass

2.ssh 密鑰

pass

二、線程,進程

定義:

進程: 是對各種資源管理的集合,qq 要以一個整體的形式暴露給操作系統管理,裏面包含對各種資源的調用,內存的管理,網絡接口的調用等
線程: 是操作系統最小的調度單位, 是一串指令的集合。

進程要想操作CPU,就必須要創建一個線程(進程中至少包含一個線程)

區別:

1.線程共享內存空間(共享數據等),進程的內存空間是獨立的

2.同一進程的線程之間可以相互交流 ,2個進程之間的交流必須通過一個中間代理

3.線程可以操作和控制其他線程(同一進程下),進程只能操作和控制子進程。

技術分享技術分享

對主線程的更改可能會影響到其他線程的工作,對父進程的更改(除非關閉)不會影響子進程。(子進程還可以派生子進程)

各種鎖

1.線程鎖(我就瞎幾把寫了一下,具體代碼看這:http://www.cnblogs.com/alex3714/articles/5230609.html)

技術分享

技術分享

2.遞歸鎖(了解就行,一般用不到)

說白了就是在一個大鎖中還要再包含子鎖

 1 import threading,time
 2 
 3  
 4 
 5 def run1():
 6 
 7     print("grab the first part data")
 8 
 9     lock.acquire()
10 11 global num 12 13 num +=1 14 15 lock.release() 16 17 return num 18 19 def run2(): 20 21 print("grab the second part data") 22 23 lock.acquire() 24 25 global num2 26 27 num2+=1 28 29 lock.release() 30 31 return num2 32 33 def run3(): 34 35 lock.acquire()
36 37 res = run1() 38 39 print(--------between run1 and run2-----) 40 41 res2 = run2() 42 43 lock.release() 44 45 print(res,res2) 46 47 48 49 50 51 if __name__ == __main__: 52 53 54 55 num,num2 = 0,0 56 57 lock = threading.RLock() 58 59 for i in range(10): 60 61 t = threading.Thread(target=run3) 62 63 t.start() 64 65 66 67 while threading.active_count() != 1: 68 69 print(threading.active_count()) 70 71 else: 72 73 print(----all threads done---) 74 75 print(num,num2)

3.信號量鎖(Semaphore)
互斥鎖 同時只允許一個線程更改數據,而Semaphore是同時允許一定數量的線程更改數據 ,比如廁所有3個坑,那最多只允許3個人上廁所,後面的人只能等裏面有人出來了才能再進去。

 1 import threading,time
 2 
 3 def run(n):
 4     semaphore.acquire()    #信號量鎖
 5     time.sleep(1)
 6     print("run the:",n)
 7     semaphore.release()    #解鎖
 8 
 9 
10 if __name__==__main__:
11     semaphore=threading.BoundedSemaphore(5)    #設定只有5個坑
12     for i in range(20):
13         t=threading.Thread(target=run,args=(i,))
14         t.start()
15 
16 while threading.active_count()!=1:
17     pass
18 else:
19     print(123)

信號量(event)

信號量只有設定(event.set())和沒設定(event.clear())兩種

event.wait()  #等待設定

event.is_set()  #判斷是否設定

紅綠燈例子

 1 import time,threading
 2 
 3 event = threading.Event()
 4 
 5 def lighter():
 6     count=0
 7     event.set()     #設定
 8     while True:
 9         if count<10 and count>=5:
10             event.clear()   #清除設定
11             print("\033[41;1m紅燈\033[0m")
12             time.sleep(1)
13         elif count>10:
14             count=0
15             event.set()
16         else:
17             print("\033[42;1m綠燈\033[0m")
18             time.sleep(1)
19         count+=1
20 
21 def car(name):
22     while True:
23         if event.is_set():  #判斷是否設定
24             print("\033[32;1m[%s] run...\033[0m"%name)
25             time.sleep(1)
26         else:
27             print(\033[31;1m [%s] stop...%name)
28             event.wait()    #等待設定
29             print(\033[33;1m [%s]走咯%name)
30 
31 
32 light=threading.Thread(target=lighter,)
33 
34 light.start()
35 
36 car1=threading.Thread(target=car,args=(Tesla,))
37 
38 car1.start()

未完待續

三、隊列(queue)

隊列的好處(作用):解耦,提高效率。

技術分享

生產者消費者模型(很簡單,沒啥可說的)

 1 import threading,time
 2 
 3 import queue
 4 
 5 q = queue.Queue(maxsize=10)
 6 
 7 def Producer(name):
 8     count = 1
 9     while True:
10         q.put("骨頭%s" % count)
11         print("生產了骨頭",count)
12         count +=1
13         time.sleep(0.1)
14 
15 
16 
17 def  Consumer(name):
18     #while q.qsize()>0:
19     while True:
20         print("[%s] 取到[%s] 並且吃了它..." %(name, q.get()))
21         time.sleep(1)
22 
23 
24 
25 p = threading.Thread(target=Producer,args=("Alex",))
26 c = threading.Thread(target=Consumer,args=("ChengRonghua",))
27 c1 = threading.Thread(target=Consumer,args=("王森",))
28 
29 
30 p.start()
31 c.start()
32 c1.start()

python學習——day9(ssh,線程和進程,信號量,隊列,生產者消費者模型) Alex地址:http://www.cnblogs.com/alex3714/articles/5230609.html