當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python threading stack_size()用法及代碼示例

Python threading.stack_size() 方法

stack_size() 是 Python 中線程模塊的內置方法。它用於返回創建新線程時所需的線程堆棧大小。

模塊:

    import threading

用法:

    stack_size([size])

參數:

  • size:它是一個可選參數,它指定用於後續創建的線程的堆棧大小。它的值必須是 0 或任何正整數;默認值為 0。

返回值:

這個方法的返回類型是<class 'int'>,它設置創建新線程時要使用的線程的堆棧大小。

例:

# Python program to explain the use of 
# stack_size()  method in Threading Module

import time
import threading

def thread_1(i):
    time.sleep(5)
    print("Value by Thread-1:",i)
    print()

def thread_2(i):
    time.sleep(4)
    print("Value by Thread-2:",i)
    print()
    
def thread_3(i):
    print("Value by Thread-3:",i)
    print()
    
def thread_4(i):
    time.sleep(1)
    print("Value by Thread-4:",i)
    print()        

# Creating sample threads 
thread1 = threading.Thread(target=thread_1, args=(100,))
thread2 = threading.Thread(target=thread_2, args=(200,))
thread3 = threading.Thread(target=thread_3, args=(300,))
thread4 = threading.Thread(target=thread_4, args=(400,))

print(threading.stack_size())

# Starting the threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()

輸出

0
Value by Thread-3:300

Value by Thread-4:400

Value by Thread-2:200

Value by Thread-1:100


相關用法


注:本文由純淨天空篩選整理自 Python threading Module | stack_size() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。