×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python threading stack_size() Method with Example

Python threading.stack_size() Method: In this tutorial, we will learn about the stack_size() method of threading module in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 23, 2023

Python threading.stack_size() Method

The threading.stack_size() is an inbuilt method of the threading module, it is used to return the thread stack size needed while creating new threads.

Module

The following module is required to use stack_size() method:

import threading

Syntax

The following is the syntax of stack_size() method:

stack_size([size])

Parameter(s)

The following are the parameter(s):

  • size: It is an optional parameter, which specifies the stack size to be used for subsequently created threads. Its value must be 0 or any positive integer; the default value is 0.

Return Value

The return type of this method is <class 'int'>, it sets the stack size for a thread to be used while creating new threads.

Example of threading.stack_size() Method in Python

# 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()

Output

0
Value by Thread-3: 300

Value by Thread-4: 400

Value by Thread-2: 200

Value by Thread-1: 100

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.