PYTHON学习之路_PYTHON基础(10)

时间:2021-12-28 13:39:47

学习内容:

Python进程与线程

1、线程及线程类

2、线程守护

3、线程等待

4、线程锁

5、信号量

6、timer用法

7、队列

8、事件驱动

9、生产者消费者模型

10、进程及进程同步

11、进程池

12、递归锁

13、进程间通讯

一、线程及线程类

import threading
import time class MyThread(threading.Thread):
def __init__(self,num):
threading.Thread.__init__(self)
self.num = num def run(self):
print ('running on number :%s'%self.num)
print ('threading_act',threading.active_count() )
print ('threading_cur:',threading.current_thread())
time.sleep(3) if __name__ == '__main__':
t1 = MyThread('alex')
t2 = MyThread('json')
t1.start()
t2.start()
print (t1.getName())
print (t2.getName())
print('threading_c:', threading.current_thread())

二、线程守护

import threading
import time def run(n):
time.sleep(1)
print ("thread",n)
t_list = []
for i in range(10):
t = threading.Thread(target=run,args=(i,))
t.setDaemon(True)
t.start()
t_list.append(t)
print (t_list)
print ('----main thread----')

三、线程等待

import threading
import time
def run(n):
time.sleep(1)
print ("thread",n)
t_list = []
for i in range(10):
t = threading.Thread(target=run,args=(i,))
t.start()
t_list.append(t)
for t in t_list:
print ('t_list:',t)
t.join() #t.wait
print ('----main thread----')

四、线程锁

import  threading
import time l = threading.Lock()
def run(n):
global num
l.acquire() #获取锁
num += 1
time.sleep(1)
l.release() #释放锁
print(num)
time.sleep(1)
#print("thread",n)
def run2():
count = 0
while num < 9:
print ('---------',count)
count += 1
num = 0
t_list = []
for i in range(10):
t = threading.Thread(target=run, args=(i,))
t.start()
t_list.append(t)
t2 = threading.Thread(target=run2)
t2.start()
for t in t_list:
t.join()
print("--main thread---")
print(num)

五、信号量(多线程)

import threading, time

def run3(n):
semaphore.acquire()
time.sleep(1)
print('--------between run1 and run2-----',n)
semaphore.release()
if __name__ == '__main__':
num, num2 = 0, 0
semaphore = threading.BoundedSemaphore(5)
for i in range(20):
t = threading.Thread(target=run3,args=(i,))
t.start()
while threading.active_count() != 1:
pass
#print('active_count',threading.active_count())
else:
print('----all threads done---')
print(num, num2)

六、timer用法

import threading
import time
def hello():
print("hello, world")
t1 = time.time()
t = threading.Timer(3.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
t.join()
t2 = time.time()
change_time = t2 - t1
print (change_time)

七、队列

q = queue.Queue(maxsize=2)
# q.put(1)
# q.put(2)
# q.put(9)
# q.put('alex')
# q.put_nowait('jack')
# q.mutex
# q.qsize()
# q.join()
# q.task_done()
# q.empty() #判断队列是否为空
# q.full() #判断队列是否满
q=queue.LifoQueue()
q=queue.PriorityQueue()
q.put([4,'40'])
q.put([5,'50'])
q.put([1,'100'])
q.put([2,'20'])
print(q.get())
print(q.get())
print(q.get())
print(q.get())

八、事件驱动

import  threading
import time
import random
def lighter():
count = 0
while True:
if count < 30:
if not event_flag.is_set():
event_flag.set()
print ('\033[32m----green light----\033[0m:%s'%count)
elif count < 35:
print('\033[33m----green light----\033[0m:%s' %count)
elif count < 60:
if event_flag.is_set():
event_flag.clear()
print('\033[31m----green light----\033[0m:%s' %count)
else:
count = 0
count += 1
time.sleep(0.2)
def car(n):
while True:
event_flag.wait()
print ('car [%s] is running throught the lighter...'%n)
time.sleep(random.randint(0, 3))
event_flag = threading.Event()
light_flag = threading.Thread(target=lighter)
light_flag.start()
car_list = ['宝马','大奔','路虎','兰博基尼','夏利']
for n in car_list:
car_flag = threading.Thread(target=car,args=(n,))
car_flag.start()

九、生产者消费者模型

import threading
import queue
import time
def consumer(name):
while True:
print ('%s 取到 骨头 【%s】,并吃了它。'%(name,q.get()))
time.sleep(0.5)
q.task_done() #回执
def producer(name):
count = 0
#while q.qsize() < 6:
for i in range (10):
print ('%s生成了骨头 %s!'%(name,count) )
q.put(count)
count +=1
time.sleep(0.3)
q.join()
q = queue.Queue(maxsize=5)
p = threading.Thread(target=producer,args=('Alex',))
p2 = threading.Thread(target=producer,args=('shuangmei',))
c = threading.Thread(target=consumer,args=('spider',))
p.start()
p2.start()
c.start()

十、进程及进程同步

from multiprocessing import Process, Lock
#作用:输出到屏幕出现乱行,加锁保证不串行 def f(l, i):
l.acquire()
try:
print('hello world', i)
finally:
l.release()
if __name__ == '__main__':
lock = Lock()
for num in range(10):
Process(target=f, args=(lock, num)).start()

十一、进程池

from multiprocessing import Process, Lock,Pool
import time
import os
#作用:输出到屏幕出现乱行,加锁保证不串行 def f(i):
#l.acquire()
print('hello world', i)
print ('-----f:',i,os.getpid())
time.sleep(4)
#l.release()
def callback(data):
print ('exec done---',data)
print('----callback',data,os.getpid()) if __name__ == '__main__':
lock = Lock()
pool = Pool(processes=5)
for num in range(10):
#Process(target=f, args=(lock, num)).start()
#pool.apply_async(func=f, args=(lock,num), callback=Bar)
pool.apply_async(func=f, args=(num,),callback=callback)
#apply 不要用,就用apply_async即可
pool.close()
pool.join()

十二、递归锁

import threading, time
def run1():
print("grab the first part data")
lock.acquire()
global num
num += 1
lock.release()
return num
def run2():
print("grab the second part data")
lock.acquire()
global num2
num2 += 1
lock.release()
return num2
def run3():
lock.acquire()
res = run1()
print('--------between run1 and run2-----')
res2 = run2()
lock.release()
print(res, res2)
if __name__ == '__main__':
num, num2 = 0, 0
lock = threading.RLock() #若用lock 则死锁
for i in range(10):
t = threading.Thread(target=run3)
t.start()
while threading.active_count() != 1:
print('active_count',threading.active_count())
else:
print('----all threads done---')
print(num, num2)

十三、进程间通讯

from multiprocessing import Process,Queue  #进程的queue
import queue #线程的queue
def f(q):
q.put([42, None, 'hello'])
q.put(123)
if __name__ == '__main__':
#q=queue.Queue #线程的队列
q = Queue()
p = Process(target=f, args=(q,))
p.start()
print(q.get()) # prints "[42, None, 'hello']"
print (q.get())
p.join()