Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get cpu_times just before process dead #834

Closed
fontealpina opened this issue Jun 8, 2016 · 1 comment
Closed

Get cpu_times just before process dead #834

fontealpina opened this issue Jun 8, 2016 · 1 comment

Comments

@fontealpina
Copy link

I was wandering if there is a way to get the cpu time of a process just before it dies?
It could be possible with a thread that monitors it with a p.cpu_times(),
but is there a way to get it in a one shot?
Thanks

@giampaolo
Copy link
Owner

giampaolo commented Jun 8, 2016

If you need to do this for your own process (or its children) just register a signal handler (a callable) for SIGTERM (check signal.signal), then do whatever you have to do in there.
Incidentally doing that "right" can be kinda tricky, but fortunately for you I wrote a recipe exactly for doing that. =)
http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html

If you're on Windows this may be troublesome as signals don't work the same way as on UNIX (basically they suck).

If you need to do this for a PID/process which is not owned by you then there's no way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants