Re: [問題] blocking queue 實作消失

看板Python作者時間7年前 (2018/07/22 03:46), 7年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
: -- : ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 172.89.32.145 : ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1532165146.A.897.html : 推 flarehunter: wait會釋放lock 所以別人才能拿lock再notify 07/21 18:11 : → flarehunter: https://docs.python.org/2/library/threading.html 07/21 18:12 : 推 cutekid: 好奇原來的code在empty時,遇到兩個get一個put為什麼會壞 07/21 19:59 : 推 flarehunter: 因為他用if不是while 07/21 21:19 Q1 https://docs.python.org/3/library/threading.html#threading.Condition.wait This method releases the underlying lock, and then blocks until it is awakened by a notify() or notify_all() call for the same condition variable in another thread, wait()這個機制是不是 block the thread 將其改為 sleep的狀態? Q2 這是原本只用一個condition lock的code https://paste.ubuntu.com/p/dkqbMs8NKS/ (我將原來MyQueu誤用的threading拿掉, 並且在condition裡面使用Lock(),而非default RLock()) 將 if 改成while之後 兩個get一個 put的情況也可以順利跑了 C1先跑,取得lock -> 發現que empty -> wait() -> release lock C2後跑,一開始被block(因為C1 holds the lock), 直到C1 release後 -> 取得lock -> 接著發現que empty -> 於是wait() -> 然後release lock 為什麼要用while就可以順利執行呢? 如果C1 or C2第一次進入get,發現que empty 直接就被block + release lock + wait for notify while 的判斷式, len(que) == 0, 應該只會被執行一次,我的理解對嗎? Q3 上面的版本,改為while之後,似乎也跑得好好的 為什麼source code要用兩個lock呢? https://paste.ubuntu.com/p/Zjh3y6ychm/ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 172.89.32.145 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1532202398.A.B4E.html ※ 編輯: sean72 (172.89.32.145), 07/22/2018 03:47:56 ※ 編輯: sean72 (172.89.32.145), 07/22/2018 03:49:06 ※ 編輯: sean72 (172.89.32.145), 07/22/2018 03:51:04
文章代碼(AID): #1RKusUjE (Python)
討論串 (同標題文章)
文章代碼(AID): #1RKusUjE (Python)