python多线程同步机制condition
生活随笔
收集整理的這篇文章主要介紹了
python多线程同步机制condition
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
import time
def customer(cond):
t = threading.currentThread()
with cond:
# wait()方法創建了一個名為waiter的鎖,并且設置鎖的狀態為locked。這個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()方法創建了一個名為waiter的鎖,并且設置鎖的狀態為locked。這個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'
轉載于:https://www.cnblogs.com/zejin2008/p/7586683.html
總結
以上是生活随笔為你收集整理的python多线程同步机制condition的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rsync 同步文件重复拷贝问题
- 下一篇: shell文件管理jenkins构建过程