简体   繁体   English

如何中断已经在 Python (APScheduler) 中运行的函数?

[英]How to interrupt a function that is already running in Python (APScheduler)?

I have an APScheduler application running in the background and wanted to be able to interrupt certain jobs after they started.我有一个在后台运行的 APScheduler 应用程序,并希望能够在它们启动后中断某些作业。 I want to do this to have more control over the processes and to prevent them from affecting other jobs.我想这样做是为了更好地控制流程并防止它们影响其他工作。

I looked up for solutions using APScheduler itself, but what I want seems to be more like interrupting a function that is already running.我使用 APScheduler 本身寻找解决方案,但我想要的似乎更像是中断已经运行的函数。

Is there a way to do this while not turning off my program?有没有办法在不关闭我的程序的情况下做到这一点? I don't want to delete the job or to stop the application as a whole, just to stop the process at specific and unpredictable situations.我不想删除作业或停止整个应用程序,只是为了在特定和不可预测的情况下停止进程。

On APScheduler 3, the only way to do this is to set a globally accessible variable or a property thereof to some value that is then explicitly checked by the target function.在 APScheduler 3 上,唯一的方法是将全局可访问变量或其属性设置为某个值,然后由目标函数明确检查。 So your code will have to support the notion of being interrupted.所以你的代码必须支持被中断的概念。 On APScheduler 4 (not released yet, as of this writing), there is going to be better cancellation support, so coroutine-based jobs can be externally cancelled without explicitly supporting this.在 APScheduler 4(在撰写本文时尚未发布)上,将会有更好的取消支持,因此可以在外部取消基于协程的作业,而无需明确支持这一点。 Regular functions still need to have explicit checks but there will now be a built-in mechanism for that.常规函数仍然需要进行显式检查,但现在将有一个内置机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM