input
的一个进程内的全部线程之间的数据是共享的数据库
#启动多线程 from threading import Thread import time def func(i): time.sleep(1) print(i) for i in range(10): t = Thread(target=func, args=(i,)) t.start() print('这是主线程执行的') #结果 这是主线程执行的 0 2 1 3 4 5 9 8 6 7 #这里的十个数字是几乎同时被输出,说明是个线程并发
面向对象的形式启动线程网络
#用面向对象的方法开启新的线程 from threading import Thread class MyThread(Thread): #继承Thread类 def __init__(self, arg): #重写__init__方法,用于给这个类传参 super().__init__() #继承父类的__init__方法 self.arg = arg #将本身的参数赋给对象 def run(self): '''objece.start()直接调用这个方法''' self.methond() def methond(self): '''这个类中的其余方法''' print(self.arg)
global n
线程共享from multiprocessing import Process from threading import Thread import time def cal_num(i): i += 1 if __name__ == '__main__': p_list = [] start = time.time() for i in range(100): #建立100个进程执行计算 p = Process(target=cal_num, args=(i, )) p.start() p_list.append(p) for i in p_list: i.join() end = time.time() t1 = end - start start = time.time() t_list = [] for i in range(100): t = Thread(target=cal_num, args=(i,)) t.start() t_list.append(t) for i in t_list: i.join() end = time.time() t2 = end - start print('100个进程消耗时间{} \n 100个线程消耗时间{}'.format(t1, t2)) #结果 100个进程消耗时间0.19627618789672852 100个线程消耗时间0.009418964385986328
threading.current_thread()
全部进程的状况.get_ident()
查看进程的id.active_count()
查看活跃进程的数量.enumerate()
全部的进程状况放进一个列表中Lock()
的死锁现象Lock()
互斥锁,只有一把钥匙RLock()
递归锁,只要拿到一把,就等于拿到一串,必须等一串所有归还下个线程才能继续拿钥匙
死锁状况多线程
#死锁状况 from threading import Thread,Lock from time import sleep #死锁状况 from threading import Thread,Lock,RLock from time import sleep mt_lock = Lock() cz_lock = Lock() def miantao(): mt_lock.acquire() print('拿到面条了') sleep(1) cz_lock.acquire() print('拿到叉子了') print('吃面') mt_lock.release() cz_lock.release() def chazi(): cz_lock.acquire() print('拿到叉子了') sleep(1) mt_lock.acquire() print('拿到面条了') print('吃面') cz_lock.release() mt_lock.release() th1 = Thread(target=miantao, args=()) th2 = Thread(target=chazi, args=()) th1.start() th2.start()
解决死锁问题RLock()
并发
from threading import Thread, Lock, RLock from time import sleep cz_lock = mt_lock = RLock() #这里建立递归锁 def miantao(): mt_lock.acquire() print('拿到面条了') sleep(1) cz_lock.acquire() print('拿到叉子了') print('吃面') mt_lock.release() cz_lock.release() def chazi(): cz_lock.acquire() print('拿到叉子了') sleep(1) mt_lock.acquire() print('拿到面条了') print('吃面') cz_lock.release() mt_lock.release() th1 = Thread(target=miantao, args=()) th2 = Thread(target=chazi, args=()) th1.start() th2.start()
建立就为阻塞状态app
from threading import Thread, Event e = Event() def test(): '''检测数据库的连通性''' info = input('>>') if info == '1': e.set() print('数据库网络链接打开') else: print('关闭数据库网络链接') def connect_q(): '''链接数据库''' print('等待数据库网络链接') e.wait() print('数据库链接成功!') t1 = Thread(target=test) t2 = Thread(target=connect_q) t1.start() t2.start()
.acquire()
#钥匙.release()
#释放钥匙.notify(num)
#容许钥匙串有几把钥匙.wait()
#等待.notify(num)
提供钥匙数量ide
#条件 from threading import Condition, Thread def print_thrad(con, i): con.acquire() #这里也有锁 con.wait() print('第{}线程运行了'.format(i)) con.release() num = int(input('>>')) con = Condition() for i in range(10): t = Thread(target=print_thrad, args=(con, i)) t.start() con.acquire() #这里也有锁 con.notify(num) #先后必须有钥匙和锁 con.release()
用法和Threa()
一致ui
from threading import Timer def func(): print('两秒时间到了') Timer(2, func).start() print('呵呵') #结果 呵呵 两秒时间到了 #子线程两秒后才开启
.put()
放元素.put_nowait()
#放元素,没有元素报错.get()
#取出元素.get_nowait()
#没有元素报错