如何中断由 SCons 管理的长任务? [英] How do I interrupt a long task managed by SCons?
问题描述
我正在使用 SCons 来管理执行长时间计算的 Python 函数 long_task().如果我直接从脚本调用 long_task() ,那么我可以用 Ctrl-C 中断它.但是,如果 SCons 正在运行 long_task() 并且我按下 Ctrl-C,那么 long_task() 将继续运行直到它完成,并且 then SCons 显示构建中断./构建因错误而终止".在这种情况下,有没有办法让 Ctrl-C 立即停止 long_task()?
I'm using SCons to manage a Python function long_task() that carries out a long calculation. If I call long_task() directly from a script, then I can interrupt it with Ctrl-C. However, if SCons is running long_task() and I press Ctrl-C, then long_task() continues to run until it completes, and then SCons says "Build interrupted. / building terminated because of errors". Is there a way to make Ctrl-C stop long_task() immediately in this situation?
如果我在 long_task() 中使用信号处理程序来处理 SIGINT,那么它会按照我的意愿运行,但我宁愿不必在几个这样长的任务中的每一个中都这样做.如果我按 Ctrl-\,则 longtask() 和 SCons 都会立即退出,但这似乎相当激烈.
If I use a signal handler in long_task() to handle SIGINT then it behaves as I want, but I'd rather not have to do this in each of several such long tasks. If I press Ctrl-\ then both longtask() and SCons exit immediately, but this seems rather drastic.
我在 Ubuntu 18.04 下使用 SCons 3.0.1 和 Python 3.6.5.
I'm using SCons 3.0.1 and Python 3.6.5 under Ubuntu 18.04.
这是一个最小的 SConstruct 文件:
Here's a minimal SConstruct file:
#!/usr/bin/env python3
import time
def long_task(target, source, env):
print('Starting long task')
# Mimic a complex calculation
for i in range(100):
time.sleep(0.1)
print('Finishing long task')
TestBuilder = Builder(action = long_task)
env = Environment(BUILDERS = {'TestBuild': TestBuilder})
env.TestBuild('dummy', [])
推荐答案
我强烈建议您不要将长时间运行的 Python 逻辑作为构建器中的一个操作来运行.而是通过 shell 作为独立的 Python 程序运行它.这也简化了在此类程序中设置时间限制的过程,因此它不会永远运行..
I highly encourage you to not run long running python logic as an action in a builder. Instead run it via shell as a free standing python program. This also simplifies building a time limit into such program so it doesn't run forever..
在 SCons 中运行长时间运行的 python 逻辑将限制并行构建.
Running long running python logic inside SCons will limit parallel builds.
基本上不要这样做..
(顺便说一句.我是 SCons 项目联合经理)
(BTW. I'm the SCons Project Co-Manager)
这篇关于如何中断由 SCons 管理的长任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!