7

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.

ango
  • 829
  • 2
  • 10
  • 23
  • The docs say you should use `time.clock` for timing not `time.time` but gives back the processor time as well, not the execution time of a thread – Matti Lyra Nov 18 '12 at 17:37

5 Answers5

9

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.

hemflit
  • 2,819
  • 3
  • 22
  • 17
  • 1
    From what I can tell `time.clock_gettime` is not available on Windows ... – Jimbo Nov 27 '19 at 19:54
  • 1
    or Mac ... (even though the documentation states availability on Unix ...) – Jimbo Nov 27 '19 at 20:01
  • @Jimbo, indeed unavailable on Windows. As to your Mac, you're probably looking at an old Python version. – hemflit Nov 28 '19 at 01:41
  • I'm using Python 3.6.3/IPython 6.1.0 through "Anaconda custom (64 bit)". Perhaps Anaconda built it in such a way that the relevant code wasn't linked/included? – Jimbo Nov 28 '19 at 02:07
  • @Jimbo that smells like a linking problem with the underlying C libraries. I'd check if it's not a known issue with Anaconda. – hemflit Nov 28 '19 at 09:35
7

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).

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
6

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.

alqualond
  • 386
  • 3
  • 5
2

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

Add the following:

def hello():
   global stop
   stop = 1
sizzzzlerz
  • 4,277
  • 3
  • 27
  • 35
  • thanks for your comment. This solves the second problem. However, I still cant figure out how to find the runtime of a thread in my multi threaded program. Any insights here? – ango Nov 18 '12 at 19:39
  • This doesn't answer the question. It might work better as a comment on the original post. – CivFan Jun 26 '17 at 21:51
0

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.

Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
  • This is no longer accurate as soon as you have more than one thread running concurrently. Which is the point of the question. In other words, this doesn't answer the question. – CivFan Jun 26 '17 at 21:49