簡體   English   中英

使用 ThreadPoolExecutor 實現無限循環的最佳方法是什么?

[英]What is the best way to implement an infinite loop with ThreadPoolExecutor?

我的代碼使用 ThreadPoolExecutor 來處理多個任務。 主要要求之一是它可以無限期地執行。 這是我當前的實現:

def process_something():
  with ThreadPoolExecutor(max_workers=MAX_WORKERS1) as executor1:
    with ThreadPoolExecutor(max_workers=MAX_WORKERS2) as executor2:
       while True:
          func1_returns = executor1.map(func1, arg1)
          func2_returns = executor2.map(func2, arg2)
    
          # code for processing func returns
    
          time.sleep(1)

有沒有更好的方法來實現這個? 由於執行程序駐留在無限循環中,這是否可能是 memory 泄漏?

線程池已經有多個線程可以使用。 您不需要創建多個池。

def process_something():
  with ThreadPoolExecutor(max_workers=MAX_WORKERS1) as executor1:
     while True:
        func1_returns = executor1.submit(func1, arg1)
        func2_returns = executor1.submit(func2, arg2)
    
        # code for processing func returns
    
        time.sleep(1)

線程池中不應有任何 memory 泄漏。 with語句完成時,線程將被垃圾回收。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM