[英]How to handle out-of-memory error while using threads in python
我有 memory 泄漏,但我找不到解決方法。 我認為原因是因為我使用線程並且沒有以正確的方式停止/殺死它。
我有以下方法:
import threading
def worker():
if nextJobActive() and number_of_active_threads<5:
t = threading.Thread(target=startThread, args=(my_list, my_item))
t.start()
def startThread():
#do something here, which takes ~15 Min.
我在 while(true) 循環中運行worker()
方法。 在我的情況下,我總是必須啟動新線程。 但我從不停止線程。 我也不知道該怎么做。 無論如何,在我的情況下是否可以安全地停止線程?
正如您已經知道的那樣,您正在創建無窮無盡的線程,而沒有正確停止前一個線程。 要等待線程終止,有一個 .join() 方法。 這是 Thread 模塊的文檔: docs 。
import threading
def worker():
if nextJobActive() and number_of_active_threads<5:
t = threading.Thread(target=startThread, args=(my_list, my_item))
t.start()
t.join()
def startThread():
#do something here, which takes ~15 Min.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.