5.2.方式2
import multiprocessing import time g_list = list() def add_data(): for i in range(6): g_list.append(i) print("add: ",i) time.sleep(0.2) print("add_data: ",g_list) def read_data(): print("read_data",g_list) if __name__ == '__main__': # 创建添加数据的子进程 add_data_process = multiprocessing.Process(target=add_data) # 创建读取数据的子进程 read_data_process = multiprocessing.Process(target=read_data) #锁 add_data_process.daemon = True #启动子进程执行对应的任务 add_data_process.start() #阻塞函数,add_data_process执行完才会继续向下执行 add_data_process.join() #全局变量不共享 read_data_process.start()
五、线程
1.线程创建
import threading import time def task1(): t = threading.currentThread() print("task1: ",t) for i in range(5): print(" Task A ", i+1) time.sleep(0.5) def task2(): t = threading.currentThread() print("task2: ",t) for i in range(5): print(" Task B ", i+1) time.sleep(0.5) #创建线程 if __name__ == '__main__': t1 = threading.Thread(target=task1,name='t1') t2 = threading.Thread(target=task2,name='t2') print(t1) print(t2) #创建好线程后,线程并不会被执行 #必须启动线程 t1.start() t2.start() print(t1) print(t2) print("Main Thread")
2.线程执行带有参数的任务
import threading import time def task(count): for i in range(count): print("任务执行中。。。") time.sleep(0.2) else: print("任务执行完成") def task1(content , count): for i in range(count): print(content,'--',i+1) time.sleep(0.5) if __name__ == '__main__': sub_thread = threading.Thread(target=task,args=(5,)) sub_thread2 = threading.Thread(target=task1,args=('Python',5)) sub_thread.start() sub_thread2.start()
3.线程的无序性
import threading import time def task(): t = threading.current_thread() for i in range(10): print(t.name) time.sleep(0.5) if __name__ == '__main__': for i in range(5): t = threading.Thread(target=task) t.start()
4.守护主线程
没有守护线程,主线程会随即执行
import threading import time def task(): t = threading.current_thread() for i in range(5): print(t.name) time.sleep(0.5) if __name__ == '__main__': t1 = threading.Thread(target=task,name='t1') t2 = threading.Thread(target=task,name='t2') t1.start() t2.start() print("main over")
方法1
这个方法主线程不等待子线程所以会出现问题
import threading import time def task(): t = threading.current_thread() for i in range(5): print(t.name) time.sleep(0.5) if __name__ == '__main__': t1 = threading.Thread(target=task,name='t1',daemon=True) t2 = threading.Thread(target=task,name='t2',daemon=True) t1.start() t2.start() print("main over")
方法2
import threading import time def task(): t = threading.current_thread() for i in range(5): print(t.name) time.sleep(0.5) if __name__ == '__main__': t1 = threading.Thread(target=task,name='t1') t2 = threading.Thread(target=task,name='t2') t1.setDaemon(True) t2.setDaemon(True) t1.start() t2.start() print("main over")
5.阻塞线程
import threading import time def task(): t = threading.current_thread() for i in range(5): print(t.name) time.sleep(0.5) if __name__ == '__main__': t1 = threading.Thread(target=task,name='t1') t2 = threading.Thread(target=task,name='t2') t1.setDaemon(True) t2.setDaemon(True) t1.start() t2.start() t1.join() t2.join() print("main over")
6.线程间共享全局变量
import threading import time c_list = [] def add_data(): for i in range(5): c_list.append(i) print(c_list) time.sleep(0.5) def read_data(): print("read: ",c_list) if __name__ == '__main__': add_t = threading.Thread(target=add_data) read_t = threading.Thread(target=read_data) add_t.start() read_t.start()
7.多线程中共享变量的资源竞争问题
import threading import time g_num = 0 def sum_num1(): for i in range(1000000000): global g_num g_num += 1 print("sum1: ",g_num) if __name__ == '__main__': f_t = threading.Thread(target=sum_num1) f_t2 = threading.Thread(target=sum_num1) f_t.start() f_t2.start() time.sleep(3) print("main",g_num)
转存失败重新上传取消
时间片轮转的问题
8.使用线程同步解决
线程等待(阻塞)
import threading import time g_num = 0 def sum_num1(): for i in range(1000000000): global g_num g_num += 1 print("sum1: ",g_num) if __name__ == '__main__': f_t = threading.Thread(target=sum_num1) f_t2 = threading.Thread(target=sum_num1) f_t.start() #阻塞 f_t.join() f_t2.start() f_t2.join() time.sleep(3) print("main",g_num)
转存失败重新上传取消
9.互斥锁
上锁
原子操作
解锁
import threading import time #创建一个实例锁 metax_lock = threading.Lock() g_num = 0 def sum_num1(): t = threading.current_thread() for i in range(1000000000): metax_lock.acquire() global g_num g_num += 1 print(t.name,"=------=",g_num) metax_lock.release() print("sum1: ",g_num) if __name__ == '__main__': f_t = threading.Thread(target=sum_num1) f_t2 = threading.Thread(target=sum_num1) f_t.start() f_t2.start() time.sleep(3) print("main",g_num)
注意不要变成同步锁,一般都是异步执行
10.死锁
这个函数就有个死锁
import multiprocessing import threading import time c_list = [1,2,3,4325] lock = threading.Lock() def get_value(index): #锁 lock.acquire() t = threading.current_thread() if index >= len(c_list): print(f'{index} 下界太大,导致下标越界') return print(t.name,"取得第",index,'个值,值为',c_list[index]) lock.release() if __name__ == '__main__': for i in range(5): t = threading.Thread(target=get_value,args=(i,)) t.start()
我们解决掉他
import multiprocessing import threading import time c_list = [1,2,3,4325] lock = threading.Lock() def get_value(index): #锁 lock.acquire() t = threading.current_thread() if index >= len(c_list): print(f'{index} 下界太大,导致下标越界') lock.release() return print(t.name,"取得第",index,'个值,值为',c_list[index]) lock.release() if __name__ == '__main__': for i in range(5): t = threading.Thread(target=get_value,args=(i,)) t.start()
但这里其实还有问题,我们后面讨论
11.死锁哲学家问题
import multiprocessing import threading import time lock_a = threading.Lock() lock_b = threading.Lock() def PersonA(): for i in range(100): lock_a.acquire() print("A抢到第一个筷子,加一个锁") lock_b.acquire() print("A加第二个锁,强盗第二根筷子,吃一口饭") lock_b.release() print("A释放锁,放下第二根筷子") lock_a.release() print("A加第一个锁,抢到第一根筷子") def PersonB(): for i in range(100): lock_a.acquire() print("B抢到第一个筷子,加一个锁") lock_b.acquire() print("B加第二个锁,强盗第二根筷子,吃一口饭") lock_b.release() print("B释放锁,放下第二根筷子") lock_a.release() print("B加第一个锁,抢到第一根筷子") if __name__ == '__main__': pa = threading.Thread(target=PersonA) pb = threading.Thread(target=PersonB) pa.start() pb.start()
结语
python的部分快要结束了,后面要更新linux服务器和k8s的笔记,希望大家多多👍