簡體   English   中英

如何在您的計算機上運行盡可能多的線程

[英]How to run the maximum number of threads possible on your computer

如何在您的計算機上運行盡可能多的線程? 我想在我的 python 代碼中運行最大數量的線程。 我在 Windows 計算機上運行代碼。

我想到了以下解決方案:

x = 0
while 1:
    try:
        t = Thread(target=learn, args=(line,)) #writer,))
        thread_list.append(t)
        t.start()
        break
    except Exception as exp:
        time.sleep(1)
        if x == 0:
            print "Waiting for free thread",
            x = 1
        else:
            print ".",

代碼運行良好,但幾分鍾后我收到以下錯誤:

mutex.acquire() #error
with open("output.csv", "ab") as dataset_file:
    writer = csv.writer(dataset_file)
    writer.writerow(dataset)
mutex.release()

mutex.acquire()
AttributeError: 'NoneType' object has no attribute 'acquire'

有沒有人有辦法修復我的錯誤或另一種方法來打開最大數量的線程? 謝謝。

您可以在 Windows 上運行的最大線程數取決於您擁有多少 memory。 Windows 中的每個線程默認消耗 1MB 的 RAM 用於堆棧(加上更多用於其上下文和微小的內核模式堆棧)。 對於 32 位應用程序,您可以創建的最大線程數約為 1800,前提是您不分配任何其他 memory。 換句話說,您可以創建多少線程歸結為您的應用程序有多少可用的 memory。

但是,您不應該創建太多線程。 通常,您的目標應該是擁有與可用內核一樣多的線程。 創建線程很昂貴,在線程之間切換也是如此。

暫無
暫無

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

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