How to find running time of a thread in Python

一世执手 提交于 2020-01-01 05:18:17

问题


I have a multi-threaded SMTP server. Each thread takes care of one client. I need to set a timeout value of 10 seconds on each server thread to terminate dormant or misbehaving clients.
I have used the time.time(), to find the start time and my checkpoint time and the difference gives the running time. But I believe it gives the system time and not the time this thread was running.
Is there a Thread local timer API in Python ?

   import threading
   stop = 0

   def hello():
     stop = 1

   t=threading.Timer(10,hello)
   t.start()
   while stop != 1:
      print stop
   print "stop changed"

This prints 0 (initial stop) in a loop and does not come out of the while loop.


回答1:


In the python documentation there is no mention of "thread timing". Either the clocks are process-wide or system-wide. In particular time.clock measures process time while time.time returns the system time.

In python3.3 the timings API was revised and improved but still, I can't see any timer that would return the process time taken by a single thread.

Also note that even if possible it's not at all easy to write such a timer. Timers are OS specific, so you would have to write a different version of the module for every OS. If you want to profile a specific action, just launch it without threads. When threaded the timing either it runs as expected, or it is a lot slower because of the OS, in which case you can't do nothing about it(at least, if you don't want to write a patch that "fixes" the GIL or removes it safely).




回答2:


Python has progressed in the 6 years since this question was asked, and in version 3.3 it's introduced a tool for exactly what was being asked for here:

time.clock_gettime(time.CLOCK_THREAD_CPUTIME_ID)

Python 3.7 additionally introduced an analogous time.clock_gettime_ns.

Detailed docs are exactly where you'd expect but the feature is pretty straightforward straight out of the box.




回答3:


The hello function's stop value is local, not the global one.

Add the following:

def hello():
   global stop
   stop = 1



回答4:


Python 3.7 has added the time.thread_time() method that seems to do what this question needs. According to the docs, it is thread-specific and excludes time spent sleeping.




回答5:


I am posting a sample code which can measure the running time of the thread, you can modify the code, so as to use with your function.

    import time
    import threading
    def hello():
        x = 0 
        while x < 100000000:
            pass
            x += 1
    start = time.clock()
    t = threading.Thread(target = hello, args = ())
    t.start() 
    t.join()
    end = time.clock()
    print "The time was {}".format(end - start)

On my system, it gave a time of 8.34 seconds.



来源:https://stackoverflow.com/questions/13442527/how-to-find-running-time-of-a-thread-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!