首页 Python python:线程进阶

python:线程进阶

1,守护线程import timefrom threading import Threaddef func():print(开始执行子线程)time.sleep(3)print(子线程执行完毕)t = Thread(target=func)t.setDaemon(True) # 进程设置守护进程 是一个属性 daemon = Truet.start()t2 = Thread(target=func)t2.star

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():
<span style=”color: #800000″>”'<span style=”color: #800000″>
检测数据库服务器的连接是否正常
<span style=”color: #800000″>”'<span style=”color: #000000″>
time.sleep(random.randint(1,2)) <span style=”color: #008000″>#<span style=”color: #008000″> 模拟连接检测的时间
e.set() <span style=”color: #008000″>#<span style=”color: #008000″> 告诉事件的标志数据库可以连接
<span style=”color: #000000″>
e =<span style=”color: #000000″> Event()
check = Thread(target=<span style=”color: #000000″>check_conn)
check.start()
conn = Thread(target=<span style=”color: #000000″>conn_mysql)
conn.start()

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()

</span><span style="color: #0000ff"&gt;while</span><span style="color: #000000"&gt; True:
    inp </span>= input(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;>>></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;)
    </span><span style="color: #0000ff"&gt;if</span> inp == <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;q</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;:
        </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;
    con.acquire()        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; condition中的锁  是递归锁</span>
    <span style="color: #0000ff"&gt;if</span> inp == <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;all</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;:
        con.notify_all()
    </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
        con.notify(int(inp))   </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 传递信号 notify(1) --> 可以放行一个线程</span>
    con.release()</pre>

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)


本文来自网络,不代表青岛站长网立场。转载请注明出处: https://www.0532zz.com/html/kaifa/python/20200729/7335.html
上一篇
下一篇

作者: dawei

【声明】:青岛站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

返回顶部