Python会观察长时间运行的进程,这么多分钟后会发出警报吗? [英] Python watch long running process, alert after so many minutes?

查看:125
本文介绍了Python会观察长时间运行的进程,这么多分钟后会发出警报吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python和Jython将应用程序部署到WebSphere.但是,我们遇到了WAS库的问题,对这些操作的调用有时可能需要30分钟才能执行.为了解决此问题,我需要能够在很长的几分钟后保持进程是否仍在执行的标签,如果是,则发送警报.

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?

推荐答案

Python具有内置的 Timer 类,它将为您处理线程事务:

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

陷阱:after_30_minutes将在单独的线程中运行,因此与任何线程一样,您必须确保其操作不会干扰其他线程(尽管您可以操作简单的数据结构而不会出现问题) CPython中的全局解释器锁定.)

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

这篇关于Python会观察长时间运行的进程,这么多分钟后会发出警报吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆