简体   繁体   English

Python相当于Perl $ ^ BASETIME

[英]Python equivalent of Perl $^BASETIME

I have a python module whose responsibility is to interpolate timestamp. 我有一个python模块,其职责是插入时间戳。 I have a use case where I want to get process start time whenever a function in this module is called. 我有一个用例,我想在调用此模块中的函数时获取进程启动时间。

In Perl equivalent of this module, I used $^BASETIME, $^T to get the process start time. 在这个模块的Perl等价物中,我使用$ ^ BASETIME,$ ^ T来获取进程的开始时间。 Is there a similar way in python also? python中也有类似的方式吗?

I referred to this question ( How to retrieve the process start time (or uptime) in python ). 我提到了这个问题( 如何在python中检索进程开始时间(或正常运行时间) )。 Is there any other way except using ps? 除了使用ps之外还有其他方法吗?

import time

def get_process_start_time():
    return time.time()

Upon more deeper investigation, I found that I can use 'create_time' method of Process to get start time of process anywhere in the code. 经过更深入的调查,我发现我可以使用Process的'create_time'方法来获取代码中任何位置的进程的开始时间。

import os
import psutil

psutil.Process(os.getpid()).create_time()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM