在多處理過程之間傳遞資料

因為在兩個執行緒之間處理時資料是敏感的(認為併發讀取和併發寫入可能相互衝突,導致競爭條件),所以製作了一組唯一物件,以便於線上程之間來回傳遞資料。任何真正的原子操作都可以線上程之間使用,但堅持使用 Queue 總是安全的。

import multiprocessing
import queue
my_Queue=multiprocessing.Queue() 
#Creates a queue with an undefined maximum size
#this can be dangerous as the queue becomes increasingly large
#it will take a long time to copy data to/from each read/write thread

大多數人會建議在使用佇列時,始終將佇列資料放在 try:except:block 而不是使用 empty。但是,對於跳過掃描週期無關緊要的應用程式(資料可以在將狀態從 queue.Empty==True 翻轉到 queue.Empty==False 時放入佇列中),通常最好在我稱之為 Iftry 塊的地方放置讀寫訪問許可權,因為’if’語句在技術上比捕獲異常更高效。

import multiprocessing
import queue
'''Import necessary Python standard libraries, multiprocessing for classes and queue for the queue exceptions it provides'''
def Queue_Iftry_Get(get_queue, default=None, use_default=False, func=None, use_func=False):
    '''This global method for the Iftry block is provided for it's reuse and 
standard functionality, the if also saves on performance as opposed to catching
 the exception, which is expencive.
        It also allows the user to specify a function for the outgoing data to use,
 and a default value to return if the function cannot return the value from the queue'''
        if get_queue.empty():
            if use_default:
                return default
        else:
            try:
                value = get_queue.get_nowait()
            except queue.Empty:
                if use_default:
                    return default
            else:
                if use_func:
                    return func(value)
                else:
                    return value
    def Queue_Iftry_Put(put_queue, value):
        '''This global method for the Iftry block is provided because of its reuse 
and 
standard functionality, the If also saves on performance as opposed to catching
 the exception, which is expensive.
        Return True if placing value in the queue was successful. Otherwise, false'''
        if put_queue.full():
            return False
        else:
            try:
                put_queue.put_nowait(value)
            except queue.Full:
                return False
            else:
                return True