2012-08-31 29 views
0

我瞭解龍捲風corountine的機制,但在這裏協同程序鎖是我無法找出問題,請給我一個手如何實現在龍捲風

考慮這個日常業務:這裏有5個數據庫操作

#operation 1 
#use asynchronous method ,doesn't matter 
#switch to other coroutine 

#operation 2 
#use asynchronous method ,doesn't matter 
#switch to other coroutine 


#operation 3 
#use asynchronous method , but i'll use the result do 
#some process then update in operation 4 
#switch to other coroutine 


#operation 4 
#use asynchronous method ,doesn't matter 
#switch to other coroutine 


#operation 5 
#use asynchronous method ,doesn't matter 
#switch to other coroutine 

,你可以看到,我不希望任何其他相關協程也更新到每個人的操作3和操作4之間的同一個表或相同的記錄,它會讓髒讀和write.in其他字

#coroutine 1 operation 3 
#coroutine 2 operation 3 
#coroutine 1 operation 4 
#coroutine 2 operation 4 

將不被接受,正確的順序應該是

#coroutine 1 operation 3 
#coroutine 1 operation 4 
#coroutine 2 operation 3 
#coroutine 2 operation 4 

我可以在操作使用3塊的方法,但會阻礙整個服務器,我希望主循環將不執行特定的協同程序,直到我告訴他們釋放。

回答

0

在我想完了之後,這很愚蠢。

這是一個單線程程序的做法

global flag 
while flag: 
    do some asynchronous empty callback 
flag = True 

#operation 3 
#use asynchronous method , but i'll use the result do 
#some process then update in operation 4 
#switch to other coroutine 

#operation 4 
#use asynchronous method ,doesn't matter 
#switch to other coroutine 

flag = False 

做真的很簡單和基本。