Python:进程运行的时间

标签 python process

我想做这样的事情:

try:
    pid = int(file(lock_file, "r").read())
    print "%s exists with pid: %s" % (lock_file, pid)
    if not check_pid(pid):
        print "%s not running. Phantom lock file? Continuing anyways" % pid
    elif wall_time(pid) > 60 * 5:
        print "%s has been running for more than 5 minutes. Killing it" % pid
        os.kill(pid)
    else:
        print "Exiting"
        sys.exit()
except IOError:
    pass

lock = file(lock_file, "w")
lock.write("%s" % os.getpid())
lock.close()

如何实现 wall_time?我必须从 /proc 读取还是有更好的方法?

最佳答案

或许您可以查看锁定文件的创建时间。这不能保证是正确的,但在大多数情况下都是正确的(而且弄错的后果很小)。

关于Python:进程运行的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1899922/

相关文章:

java - JUnit 测试数据库失败?

python - 在 Excel 或 Python 中将数字除以日期

c - fork() 语句中的进程数量(包括程序)

c++ - windows EnumProcesses 某些进程名称为 <unknown>

c++ - execl 不运行编程

Django:我应该启动一个单独的过程吗?

python - python中的导入规则是什么

python - 子进程模块 : using the call method with tempfile objects

python - Django 支付处理的良好解决方案

python - 我的游戏结束代码中的碰撞在 pygame 中无法正常工作