python多线程同步机制condition
生活随笔
收集整理的這篇文章主要介紹了
python多线程同步机制condition
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
import time
def customer(cond):
t = threading.currentThread()
with cond:
# wait()方法創(chuàng)建了一個(gè)名為waiter的鎖,并且設(shè)置鎖的狀態(tài)為locked。這個(gè)waiter鎖用于線程間的通訊
cond.wait()
print '{}: Resource is available to consumer'.format(t.name)
def producer(cond):
t = threading.currentThread()
with cond:
print '{}: Making resource available'.format(t.name)
cond.notifyAll()
if __name__ == "__main__":
cond = threading.Condition()
c1 = threading.Thread(target=customer, args=(cond,), name='c1')
c2 = threading.Thread(target=customer, args=(cond,), name='c2')
p1 = threading.Thread(target=producer, args=(cond,), name='p1')
c1.start()
c2.start()
p1.start()
print 'Main end'
# -*- coding: utf-8 -*-
import threading
import time
def customer(cond):
t = threading.currentThread()
with cond:
# wait()方法創(chuàng)建了一個(gè)名為waiter的鎖,并且設(shè)置鎖的狀態(tài)為locked。這個(gè)waiter鎖用于線程間的通訊
cond.wait()
print '{}: Resource is available to consumer'.format(t.name)
def producer(cond):
t = threading.currentThread()
with cond:
print '{}: Making resource available'.format(t.name)
cond.notifyAll()
if __name__ == "__main__":
cond = threading.Condition()
c1 = threading.Thread(target=customer, args=(cond,), name='c1')
c2 = threading.Thread(target=customer, args=(cond,), name='c2')
p1 = threading.Thread(target=producer, args=(cond,), name='p1')
c1.start()
c2.start()
p1.start()
print 'Main end'
轉(zhuǎn)載于:https://www.cnblogs.com/zejin2008/p/7586683.html
總結(jié)
以上是生活随笔為你收集整理的python多线程同步机制condition的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: rsync 同步文件重复拷贝问题
- 下一篇: shell文件管理jenkins构建过程