[英]How do I timestamp simultaneous function calls in Python?
I have a read function in a module. 我在模块中具有读取功能。
If I perform that function simultaneously I need to timestamp it. 如果同时执行该功能,则需要为其加时间戳。
How do I do this? 我该怎么做呢?
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: 与Swaroop的示例不同之处在于:
#!/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
如果您有兴趣,可以在python Wiki上找到有关装饰器的大量信息 。
Some example code by Terik Ziade Terik Ziade的一些示例代码
(more polished version, which uses timeit module, can be found in his recent book Expert Python Programming) (使用timeit模块的更完善的版本可以在他的最新著作《专家Python编程》中找到)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.