2013-05-20 32 views
2

我有一個python應用程序,它產生了一個單獨的進程來完成一些工作(由於GIL(全局解釋器鎖),我遇到了使用線程的性能問題)。 現在我在Python中使用什麼方法來跨進程同步共享資源?在Python中的多個進程之間的同步

我將數據移入隊列,並且產生進程在接收到來自該隊列的數據時完成工作。但我需要能夠保證數據以有序的方式出來,與複製的順序相同,所以我需要保證任何時候只有一個進程可以從隊列中讀/寫。 我該如何做到最好?

謝謝, 羅恩

+0

http://en.wikipedia.org/wiki/Semaphore_(programming)? – Noelkd

+0

Noelkd,謝謝 - 我發現這個:http://docs.python.org/release/2.4.2/lib/semaphore-examples.html會這樣做嗎? – cerr

+0

是的,這是正確的應該是一些更新的文檔。可能寫一個快速的例子,如果沒有人也擊敗了我! – Noelkd

回答

1

我想你需要一個信號量,檢查此示例代碼:

import threading 
import datetime 


class ThreadClass(threading.Thread): 
    def run(self): 
     now = datetime.datetime.now() 
     pool.acquire() 
     print "%s says hello, World! at time: %s" % (self.getName(),now) 
     pool.release() 


pool = threading.BoundedSemaphore(value=1) 


for i in range(10): 
     t = ThreadClass() 
     t.start() 

有這樣的輸出:

Thread-1 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-2 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-3 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-4 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-5 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-6 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-7 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-8 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-9 says hello, World! at time: 2013-05-20 18:57:47.609000 
Thread-10 says hello, World! at time: 2013-05-20 18:57:47.609000 

凡爲:

import threading 
import datetime 


class ThreadClass(threading.Thread): 
    def run(self): 
     now = datetime.datetime.now() 
     print "%s says hello, World! at time: %s" % (self.getName(),now) 




for i in range(10): 
     t = ThreadClass() 
     t.start() 

有這樣的輸出:

Thread-1 says hello, World! at time: 2013-05-20 18:58:05.531000Thread-2 says hello, World! at time: 2013-05-20 18:58:05. 
531000 

Thread-4 says hello, World! at time: 2013-05-20 18:58:05.531000Thread-3 says hello, World! at time: 2013-05-20 18:58:05 
.531000 

Thread-6 says hello, World! at time: 2013-05-20 18:58:05.531000Thread-5 says hello, World! at time: 2013-05-20 18:58:05 
.531000 

Thread-8 says hello, World! at time: 2013-05-20 18:58:05.531000Thread-7 says hello, World! at time: 2013-05-20 18:58:05 
.531000 

Thread-10 says hello, World! at time: 2013-05-20 18:58:05.531000Thread-9 says hello, World! at time: 2013-05-20 18:58:0 
5.531000 

因此,與Python中的BoundedSemaphore可以確保之前有人寫你的隊列,他們必須有信號量。這並不能確保您的結果以正確的順序添加到隊列中。

編輯:

如果你要做到這一點,並維持秩序你會需要這樣的事:

import multiprocessing 
import datetime 
import random 
import time 

def funfun(number): 
    time.sleep(random.randint(0,10)) 
    now = datetime.datetime.now() 
    return "%s says hello, World! at time: %s" % (number,now) 

if __name__ == "__main__": 
    pool = multiprocessing.Pool(10) 
    for item in pool.imap(funfun,[i for i in range(10)]): 
     print item 

它打印:

0 says hello, World! at time: 2013-05-21 00:38:48.546000 
1 says hello, World! at time: 2013-05-21 00:38:55.562000 
2 says hello, World! at time: 2013-05-21 00:38:47.562000 
3 says hello, World! at time: 2013-05-21 00:38:51.578000 
4 says hello, World! at time: 2013-05-21 00:38:50.578000 
5 says hello, World! at time: 2013-05-21 00:38:48.593000 
6 says hello, World! at time: 2013-05-21 00:38:52.593000 
7 says hello, World! at time: 2013-05-21 00:38:48.593000 
8 says hello, World! at time: 2013-05-21 00:38:50.593000 
9 says hello, World! at time: 2013-05-21 00:38:51.609000 

所以用這個你可以只以正確的順序附加到隊列中,並且作業將等待輪到他們加入隊列。