Open In App

Python time.pthread_getcpuclockid() Function

Last Updated : 21 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The pthread_getcpuclockid() function returns the clock id of the thread-specific CPU-time clock for the specified thread_id. The thread ids are obtained from the different threads that are running being used by that program. The thread ids can be obtained using the ‘ident’ field of the threading class in python. A simple snippet of creating a class, declaring it as a thread, and getting its id is shown below:

Python3




from threading import Thread
  
class Hello(Thread):
    def run(self):
        pass
        # code to be executed in this thread
  
helloobj = Hello()
helloobj.start()
print(helloobj.ident)


Output:

140388898805504

Syntax: 

time.pthread_getcpuclockid(thread_id)

The time.pthread_getcpuclockid(int) function takes an integer parameter which is the thread id and returns the CPU clock id as an integer. The complete snippet of the program to get the CPU clock id is as follows:

When using this method is that the thread should be started before calling the ‘ident’ field.

Python3




from threading import Thread
import time
  
class Hello(Thread):
    def run(self):
        print("thread 1")
  
  
helloobj = Hello()
helloobj.start()
print(time.pthread_getcpuclockid(helloobj.ident))


Output:

thread 1
-24186

Note: The first function that will run when the start() method is called is the run() method. It is important to declare the run() method inside the class because it will be called internally by the start() methods. Also, python3 or higher is absolutely necessary for this function to work or you are likely to face an AttributeError.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads