python:线程进阶
1,守护线程 threading (3(t = Thread(target=<span style="color: #000000">func)t.setDaemon(True) <span style="color: #008000">#<span style="color: #008000"> 进程设置守护进程 是一个属性 daemon = True <span style="color: #000000">t.start() t2 = Thread(target=<span style="color: #000000">func) t2.start() t2.join() <span style="color: #008000">#<span style="color: #008000"> 等待t2结束 # 守护线程 守护进程 都是等待主进程或者主线程中的代码 执行完毕# t2 = Thread(target=func)# t2.start() ---> 代码执行完毕# 守护线程就结束了# 主线程还没结束 等待t2继续执行# t2执行完毕 主线程结束# t2 = Thread(target=func)# t2.start()# t2.join() # 等待t2结束 执行完这句话代码才执行完毕# t2线程执行完毕# 主线程中没有代码了,守护线程结束 2,锁 threading threading 2= n time.sleep(0.01= temp-1 n = 100<span style="color: #000000">lock =<span style="color: #000000"> Lock() t_lst =<span style="color: #000000"> [] <span style="color: #0000ff">for i <span style="color: #0000ff">in range(100<span style="color: #000000">): t = Thread(target=<span style="color: #000000">func) t.start() t_lst.append(t) [t.join() <span style="color: #0000ff">for t <span style="color: #0000ff">in<span style="color: #000000"> t_lst] <span style="color: #0000ff">print<span style="color: #000000">(n) <span style="color: #008000">#<span style="color: #008000"> GIL 不是锁数据 而是锁线程<span style="color: #008000"> <span style="color: #008000"> 在多线程中 特殊情况 仍然要加锁 对数据3,死锁 threading threading = kz = RLock() (%(%(%<span style="color: #0000ff">def<span style="color: #000000"> eat2(name):m.acquire() <span style="color: #008000">#<span style="color: #008000"> 没有钥匙 <span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">%s拿到菠萝蜜干了<span style="color: #800000">' %<span style="color: #000000"> name) time.sleep(1<span style="color: #000000">) kz.acquire() <span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">%s拿到筷子了<span style="color: #800000">' %<span style="color: #000000"> name) <span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">%s吃菠萝蜜干<span style="color: #800000">' %<span style="color: #000000"> name) kz.release() m.release() Thread(target=eat,args=('兔子',)).start()Thread(target=eat2,args=('rabbit',)).start()Thread(target=eat,args=('大橘',args=('Orange_Cat',)).start() 在不同的线程中 恰好要对这两个数据进行操作所以就出现了死锁。4,信号量threading threading (% ( % sem = Semaphore(5) i range(20 Thread(target=func,args= 5,事件 threading threading conn_mysql(): count = 1 e.is_set(): count>3(%+= 10.5) ()<span style="color: #0000ff">def<span style="color: #000000"> check_conn(): 6,条件 ( %<span style="color: #0000ff">if <span style="color: #800080">name == <span style="color: #800000">'<span style="color: #800000">main<span style="color: #800000">'<span style="color: #000000">:con = threading.Condition() <span style="color: #008000">#<span style="color: #008000"> 条件 = 锁 + wait的功能 <span style="color: #0000ff">for i <span style="color: #0000ff">in range(10<span style="color: #000000">): t = threading.Thread(target=run,)) t.start()
7,计时器 threading <span style="color: #0000ff">def<span style="color: #000000"> hello():<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">hello,world<span style="color: #800000">"<span style="color: #000000">) <span style="color: #0000ff">while True: <span style="color: #008000">#<span style="color: #008000"> 每隔一段时间要开启一个线程 t = Timer(10,hello) <span style="color: #008000">#<span style="color: #008000"> 定时开启一个线程,执行一个任务 <span style="color: #008000">#<span style="color: #008000"> 定时 : 多久之后 单位是s <span style="color: #008000">#<span style="color: #008000"> 要执行的任务 :函数名 t.start() 8,队列 = queue.PriorityQueue() pq.put((1,1,15,2,(pq.get())9,concurrent与线程池和回调函数 concurrent 1,3 n* = futures.ThreadPoolExecutor(5) thread_pool.map(funcname,range(10)) f_lst = i range(10= thread_pool.submit(funcname,i) f f_lst: (f.result()) thread_pool.submit(funcname,1).add_done_callback(call)(编辑:阜阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |