介紹
主動物件模式是一種同時設計模式,它將方法執行與方法呼叫解耦。此模式的主要目標是透過在單獨的執行緒中執行操作來引入異步為,同時向客戶端提供同步介面。這是透過訊息傳遞、請求佇列和調度機制的組合來實現的。
關鍵零件
- Proxy:代表客戶端的公共介面。更簡單地說,這就是客戶端將要互動的內容。它將方法呼叫轉換為對活動物件的請求。
- 調度器:管理請求佇列並決定請求執行的順序。
- Servant:包含被呼叫方法的實際實作。這就是實際計算邏輯的所在。
- 啟動佇列:儲存來自代理程式的請求,直到排程器處理它們。
- Future/Callback:非同步計算結果的佔位符。
工作流程
- 客戶端呼叫代理上的方法。
- 代理程式建立請求並將其放入啟動佇列中。
- 調度程序接收請求並將其轉發給servant執行。
- 結果透過 future 物件傳回給客戶端。
使用案例
- 需要可預測執行模式的即時系統。
- GUI 應用程式保持主執行緒回應。
- 用於處理非同步請求的分散式系統。
執行
假設我們需要進行計算,可能是 API 呼叫、資料庫查詢等。我不會實現任何異常處理,因為我太懶了。
def compute(x, y): time.sleep(2) # Some time taking task return x + y
沒有活動對像模式
以下是我們如何在不使用主動物件模式的情況下處理並發請求的範例。
import threading import time def main(): # Start threads directly results = {} def worker(task_id, x, y): results[task_id] = compute(x, y) print("Submitting tasks...") thread1 = threading.Thread(target=worker, args=(1, 5, 10)) thread2 = threading.Thread(target=worker, args=(2, 15, 20)) thread1.start() thread2.start() print("Doing other work...") thread1.join() thread2.join() # Retrieve results print("Result 1:", results[1]) print("Result 2:", results[2]) if __name__ == "__main__": main()
上述方法的缺點
執行緒管理:直接管理執行緒會增加複雜性,尤其是隨著任務數量的增加。
缺乏抽象:客戶端負責管理執行緒的生命週期,將任務管理與業務邏輯耦合。
可擴充性問題:如果沒有適當的佇列或排程機制,就無法控制任務執行順序。
回應能力有限:客戶端必須等待執行緒加入才能存取結果。
使用主動物件模式實現
下面是主動物件模式的 Python 實現,使用執行緒和佇列來執行與上面相同的操作。我們將一一介紹每個部分:
MethodRequest: 封裝方法、參數和用於儲存結果的 Future。
def compute(x, y): time.sleep(2) # Some time taking task return x + y
調度程式:在單獨的執行緒中持續處理來自activation_queue的請求。
import threading import time def main(): # Start threads directly results = {} def worker(task_id, x, y): results[task_id] = compute(x, y) print("Submitting tasks...") thread1 = threading.Thread(target=worker, args=(1, 5, 10)) thread2 = threading.Thread(target=worker, args=(2, 15, 20)) thread1.start() thread2.start() print("Doing other work...") thread1.join() thread2.join() # Retrieve results print("Result 1:", results[1]) print("Result 2:", results[2]) if __name__ == "__main__": main()
Servant:實作實際邏輯(例如,計算方法)。
class MethodRequest: def __init__(self, method, args, kwargs, future): self.method = method self.args = args self.kwargs = kwargs self.future = future def execute(self): try: result = self.method(*self.args, **self.kwargs) self.future.set_result(result) except Exception as e: self.future.set_exception(e)
Proxy:將方法呼叫轉換為請求並傳回結果的 Future。
import threading import queue class Scheduler(threading.Thread): def __init__(self): super().__init__() self.activation_queue = queue.Queue() self._stop_event = threading.Event() def enqueue(self, request): self.activation_queue.put(request) def run(self): while not self._stop_event.is_set(): try: request = self.activation_queue.get(timeout=0.1) request.execute() except queue.Empty: continue def stop(self): self._stop_event.set() self.join()
客戶端:非同步提交任務並在需要時檢索結果。
import time class Servant: def compute(self, x, y): time.sleep(2) return x + y
優點
- 解耦介面:客戶端可以呼叫方法而無需擔心執行細節。
- 響應性:非同步執行確保客戶端保持回應。
- 可擴充性:支援多個並發請求。
缺點
- 複雜度:增加架構複雜度。
- 開銷:需要額外的資源來管理執行緒和佇列。
- 延遲:非同步處理可能會引入額外的延遲。
結論
主動物件模式是用於管理多執行緒環境中的非同步操作的強大工具。透過將方法呼叫與執行分離,它可以確保更好的回應能力、可擴展性和更清晰的程式碼庫。雖然它具有一定的複雜性和潛在的效能開銷,但它的好處使其成為需要高並發和可預測執行的場景的絕佳選擇。然而,它的使用取決於當前的特定問題。與大多數模式和演算法一樣,不存在一刀切的解決方案。
參考
維基百科 - 活動對象
以上是並發模式:主動對象的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具