Python Thread-based parellelism: active_count()














































Python Thread-based parellelism: active_count()



Python threading.active_count() Method


  active_count() is an inbuilt  method of the threading module in Python. 

    It is used to return the number of Thread objects that are active at any instant.

  Syntax: 

              active_count() 

  Module:

                import threading  

   Return value:

            The return type of this method is <class 'int'>, it returns the number of active Thread class objects at any instant.

 

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

import time
import threading

def thread_1(i):
time.sleep(2)
print("Number of active threads:", threading.active_count())
print('Value by Thread 1:', i)

def thread_2(i):
time.sleep(5)
print("Number of active threads:", threading.active_count())
print('Value by Thread 2:', i)

def thread_3(i):
print("Number of active threads:", threading.active_count())
print("Value by Thread 3:", i)

# Creating sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
thread3 = threading.Thread(target=thread_3, args=(3,))

print("Number of active threads in the starting:", threading.active_count())
print("The active threads in the starting is 1 which is the main thread that executes till the program runs")

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

Output:-

Number of active threads in the starting: 5
The active threads in the starting is 1 which is the main thread that executes till the program runs
Number of active threads: 8
Value by Thread 3: 3
Number of active threads: 7
Value by Thread 1: 1
Number of active threads: 6
Value by Thread 2: 2



In next article , we will learn about  threading .current_thread()

Link - javascript:nicTemp();


More Articles of Diksha Kumari:

Name Views Likes
Python : py_compile | Generate byte code 308 0
Python : py_compiler |Convert py to pyc 1157 0
Python : py_compile |Compile python sources file 287 0
Python :How to lock Critical Sections(Thread based parallelism) 249 0
Python : Locking without Deadlocks(Thread based Parallelism ) 180 0
Python : Communicating Between Threads-2(Thread based parallelism) 389 0
Python : Communicating Between Threads -1(Thread based parallelism) 176 0
Python Start and Stop a thread (Thread based parallelism) 165 0
Python: Check if a Thread has started (thread based parallelism) 144 0
Python Thread-based parallelism :main_thread() 229 0
Python Lock Class | acquire() Method(thread based parallelism) 294 0
Python Thread-based parallelism : get_native_id() 460 0
Python Thread-based parellelism: active_count() 270 1
Python Lock Class | lock() Method(thread based parallelism) 246 0
Python Thread-based parallelism : stack_size([size]) 331 1
Python Thread Local Data(thread based parallelism 265 1
Python Semaphore Objects (thread based parallelism) 264 0
Python Event class - set() method | Thread based parallelism 252 1
Python Condition object (thread based parallelism) 411 0
Python Thread based parallelism : Run() 228 0
Python Thread-based parallelism : current_thread() 254 1
Python Condition object - acquire()(thread based parallelism) 190 0
Python Thread-based parallelism : TIMEOUT_MAX () 423 0
Python Condition object - notify_all() ( thread based parallelism ) 192 0
Python Thread-based parallelism :.settrace(func) 308 0
Python difference lock and RLock object : Thread based parallelism 1083 0
Python Event class - clear() method | Thread based parallelism 188 0
Python Thread object -start() (thread based parallelism) 335 0
Python RLock Class - acquire() Method (thread based parallelism) 226 0
Python Thread-based parallelism : enumerate() 246 0
Python Thread based parallelism: get_ident() 302 1
Python Thread-based parallelism : setprofile(func) 215 0
Python RLock Class - release() Method (thread based parallelism) 194 0
Python Barrier object | Thread based parallelism 228 0
Python Condition object - notify () ( thread based parallelism ) 217 0
Python Event class - is_set() method | Thread based parallelism 173 0
Python Thread based parallelism : join() 317 0
Python thread based parallelism : set.name() 200 0
Python Event class - wait() method | Thread based parallelism 243 0
PYTHON Thread based parallism 293 1
Python Barrier method | Thread based parallelism 228 0
Python Condition object - release() ( thread based parallelism ) 184 0
Python Thread based parallelism: excepthook 1487 1
Python thread object : is_alive() (thread based parallelism) 996 0
Python Condition object - wait() ( thread based parallelism ) 215 0
Python Lock Class | release() Method(thread based parallelism) 254 0
Python Thread object (thread based parallelism) 259 0
Python Timer class - start() method | Thread based parallelism 175 1
Python thread based parallelism : get.name() 322 1
Python Timer class - cancel() method | Thread based parallelism 206 1
Python thread object : Deamon() (thread based parallelism) 241 0

Comments