Python: the amount of time on the wall that the process was running

I want to do something like this:

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

How to implement wall_time? Should I read with /procor is there a better way?

+3
source share
2 answers

Perhaps you can see the time the lock file was created. This would not be guaranteed correctly, but in most cases it would be correct (and the consequences of its improper use are minimal).

+1
source

If for some reason you do not want to use the time the lock file was modified, you can simply write it in the file:

pid, start_time = map(int, file(lock_file, "r").read().split())
...
lock.write("%s %d" % (os.getpid(), time.time()))
+1
source

Source: https://habr.com/ru/post/1725598/


All Articles