-1

I have a read function in a module.

If I perform that function simultaneously I need to timestamp it.

How do I do this?

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
user46646
  • 153,461
  • 44
  • 78
  • 84
  • please give more detail. where is the timestamp supposed to be recorded? Are you reading from a file? Are you saying that the timestamping has to be simultaneous with the read function, or that you need to timestamp if simultaneous reads occur? – John Mulder Jan 09 '09 at 05:33
  • what do you mean by timestamping a function? – hasen Jan 09 '09 at 06:22
  • what do you mean by simultaneous? – Jim Carroll Jan 09 '09 at 06:42
  • "Simultaneous" doesn't mean anything. Even if there are multiple threads, only one of them is actually scheduled at a given time. If it's across multiple processes, it's not the "same" function, it's two copies, one in each process. – S.Lott Jan 09 '09 at 11:44

4 Answers4

6

I'll offer a slightly different approach:

import time

def timestampit(func):
    def decorate(*args, **kwargs):
        decorate.timestamp = time.time()
        return func(*args, **kwargs)
    return decorate

@timestampit
def hello():
    print 'hello'


hello()
print hello.timestamp

time.sleep(1)

hello()
print hello.timestamp

The differences from Swaroop's example are:

  1. I'm using time.time() and not datetime.now() for a timestamp, because it's more suitable for performance testing
  2. I'm attaching the timestamp as an attribute of the decorated function. This way you may invoke and keep it whenever you want.
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • He is asking about simultaneous calls of the function. With this approach the slightly later call will overwrite the timestamp from the call before. –  Jan 09 '09 at 08:31
  • Yes, but this is an intentional example with the same function. He can decorate two different functions. And frankly what does simultaneous mean ?? – Eli Bendersky Jan 09 '09 at 14:38
  • 1
    Right, I'm pretty sure he did't talk about threads or other parallelization techniques. But in absence of an other explanation of what "simultaneous" might mean, I thought, I would mention it here. –  Jan 09 '09 at 17:18
2
#!/usr/bin/env python

import datetime

def timestampit(func):
    def decorate(*args, **kwargs):
        print datetime.datetime.now()
        return func(*args, **kwargs)
    return decorate

@timestampit
def hello():
    print 'hello'

hello()

# Output:
# $ python test.py 
# 2009-01-09 11:50:48.704584
# hello
Swaroop C H
  • 16,902
  • 10
  • 43
  • 50
  • I am a beginner in Python.Please dont mind of my question. Can you please explain what is happening ,Swaroop??? – user46646 Jan 09 '09 at 06:27
  • @rejinacm, We are using decorators, you can [read about it in this introduction](http://www.artima.com/weblogs/viewpost.jsp?thread=240808). – Swaroop C H Jan 09 '09 at 06:29
0

If you're interested, there's a wealth of information about decorators on the python wiki.

Jon Cage
  • 36,366
  • 38
  • 137
  • 215
0

Some example code by Terik Ziade

(more polished version, which uses timeit module, can be found in his recent book Expert Python Programming)

Mekk
  • 1,441
  • 10
  • 12