Python之路,day10-Python基础

一、进程
进程:一个程序要运行时所需的所有资源的集合
进程是资源的集合,相当于一个车间

一个进程至少需要一个线程,这个线程为主线程
一个进程里可以有多个线程

cpu cores越多,代表着你可以真正并发的线程越多

2个进程之间的数据是完全独立的,互相不能访问
1.进程lock
 1 from multiprocessing import Process, Lock
 2 
 3 
 4 def f(l, i):
 5     l.acquire()
 6     try:
 7         print('hello world', i)
 8     finally:
 9         l.release()
10 
11 
12 if __name__ == '__main__':
13     lock = Lock()
14 
15     for num in range(10):
16         Process(target=f, args=(lock, num)).start()

2.进程池

 1 from multiprocessing import Process, Lock,Pool
 2 import time
 3 
 4 def f(i):
 5     # l.acquire()
 6     # try:
 7         print('hello world  %s'%i)
 8         time.sleep(1)
 9     # finally:
10     #     l.release()
11         return i
12 
13 def Bar(data):
14     print(data)
15 if __name__ == '__main__':
16     lock = Lock()
17     pool = Pool(processes=5)
18     for num in range(10):
19         # Process(target=f, args=(lock, num)).start()
20         pool.apply_async(func=f,args=(num,),callback=Bar)
21     pool.close()
22     pool.join()

3.进程间通信

 1 from multiprocessing import Process, Queue
 2 
 3 
 4 def f(q):
 5     q.put([42, None, 'hello'])
 6 
 7 
 8 if __name__ == '__main__':
 9     q = Queue()
10     p = Process(target=f, args=(q,))
11     p.start()
12     print(q.get())  # prints "[42, None, 'hello']"
13     p.join()

3.多进程

 1 from multiprocessing import Process
 2 import time
 3 
 4 
 5 def f(name):
 6     time.sleep(2)
 7     print('hello', name)
 8 
 9 
10 if __name__ == '__main__':
11     for i in range(10):
12         p = Process(target=f, args=('bob',))
13         p.start()
14         # p.join()

 


二、线程
线程:
一个单一的指令的控制流,寄生在进程中

单一进程里的多个线程是共享数据的

多线程在涉及修改同一数据时一定要加锁

自己总结:(宏观并发,时间片切换比进程快),多核编程
1.线程
 1 import threading
 2 import time
 3 def run(n):
 4     time.sleep(1)
 5     # print(threading.get_ident())
 6     print("thread",n)
 7     print(threading.current_thread())
 8 for i in range(10):
 9     t = threading.Thread(target=run, args=(i,))
10     t.start()
11     # t.setName(i)
12     print(t.getName())
13 
14 print(threading.active_count())
15 # t = threading.Thread(target=run,args=(1,))
16 # t.start()
17 # t2 = threading.Thread(target=run,args=(2,))
18 # t2.start()
19 print(threading.current_thread())
20 time.sleep(1.5)
21 
22 print(threading.active_count())

2.线程lock.py

 1 import threading
 2 import time
 3 def run(n):
 4     global num
 5     l.acquire()#获取锁
 6     num = num + 1
 7     time.sleep(1)
 8     l.release()#释放锁
 9     print(num)
10     # print("thread",n)
11 def run2():
12     count = 0
13     while num < 9:
14         print('---', count)
15         count +=1
16 
17 l = threading.Lock()   #定义锁
18 
19 num = 0
20 t_list = []
21 for i in range(10):
22     t = threading.Thread(target=run, args=(i,))
23     t.start()
24     t_list.append(t)
25 t2 = threading.Thread(target=run2)
26 t2.start()
27 
28 for t in t_list:
29     t.join()
30 
31 print('----main Thread----')
32 print(num)

3.线程等待

 1 import threading
 2 import time
 3 def run(n):
 4     time.sleep(1)
 5     print("thread",n)
 6 
 7 t_list = []
 8 
 9 for i in range(10):
10     t = threading.Thread(target=run, args=(i,))
11     t.start()
12     t_list.append(t)
13 # t.join()
14 for t in t_list:
15     t.join()
16 
17 print('----main Thread----')

 



生产者消费者的两个主要作用
1.程序的解耦合,异步
2.提高了程序的运行效率
 1 import threading
 2 import  queue
 3 import time
 4 def consumer(name):
 5 
 6     while True:
 7         print("%s 取到骨头[%s]吃了" %(name,q.get()))
 8         time.sleep(0.5)
 9         q.task_done()
10 def producer(name):
11     count = 0
12     # while q.qsize() < 5:
13     for i in range(10):
14         print("%s生成了骨头"%name,count)
15         q.put(count)
16         count +=1
17         time.sleep(0.3)
18     q.join()
19     print("------chiwan--------")
20 q = queue.Queue(maxsize = 4)
21 
22 p = threading.Thread(target=producer,args=('alex',))
23 # p2 = threading.Thread(target=producer,args=('aaa',))
24 c = threading.Thread(target=consumer,args=('qqq',))
25 p.start()
26 # p2.start()
27 c.start()

 

posted on 2016-12-17 10:34  灵魂与梦想  阅读(240)  评论(0编辑  收藏  举报

导航