Python watch long running process, alert after so many minutes?

夙愿已清 提交于 2019-12-22 18:47:23

问题


I'm working with Python and Jython to deploy applications to WebSphere. However, we are running into an issue with the WAS libraries where calls to these actions will sometimes take up to 30 minutes to execute. In order to troubleshoot this issue, I need to be able to keep tabs on if a process is still executing after so many minutes and, if so, send an alert.

I'm assuming I will need to put the call in a separate thread and watch it, but I have no experience with multithreading. What is the best way to do so and are there any gotchas?


回答1:


Python has a built-in Timer class which will handle the threading stuff for you:

import threading

def after_2_minutes():
   if process_still_running():
       print "ALERT!!"

# start a timer in the background that waits 2 minutes
threading.Timer(2 * 60, after_2_minutes).start()

# start the process
foo.bar()

Gotchas: after_30_minutes will run in a separate thread, so as with any thread, you have to make sure that its actions don't interfere with your other threads (although you can manipulate simple data structures without problems due to the Global interpreter lock in CPython).



来源:https://stackoverflow.com/questions/9999873/python-watch-long-running-process-alert-after-so-many-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!