第一次寫 Python thread 就當機(附當機程式)


建立時間: 2022年12月30日 01:43
更新時間: 2022年12月30日 01:43

說明

最近買了一本<<Python 非同步設計使用 Asyncio>>,在我看到第二章、第一個範例建立10000個 threads,然後印出了虛擬記憶體預先配置了 80GB,秉持著一邊看一邊動手做的我也執行了這個程式,一開始發生錯誤,過沒幾秒電腦就重啟了。

程式

threadmem.py

from time import sleep
from threading import Thread

import os

threads = [
    Thread(target=lambda: sleep(60)) for i in range(10000)
]

[t.start() for t in threads]
print(f'PID = {os.getpid()}')
[t.join() for t in threads]

如果想正常運作可以把 range 設小一點。

觀看次數: 729
pythonthread執行緒
按讚追蹤 Enjoy 軟體 Facebook 粉絲專頁
每週分享資訊技術

一杯咖啡的力量,勝過千言萬語的感謝。

支持我一杯咖啡,讓我繼續創作優質內容,與您分享更多知識與樂趣!