简体   繁体   English

如何为 c++ 的 sublime 构建系统添加超时(在 Windows 上)

[英]How to add timeout (on windows) for sublime build system for c++

I have this build system, and I want to modify it so that it will automatically kill the process if it takes more than 10 seconds, how can I do so?我有这个构建系统,我想修改它,如果它需要超过 10 秒,它会自动终止进程,我该怎么做?

{
    "cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe"],
    "shell":true,
    "working_dir":"$file_path",
    "selector":"source.cpp"
}

Since a sublime-build is just executing an external process, one way to do this would be to have the build execute an external tool (or script or batch file, etc) that keeps track of time and kills itself.由于sublime-build只是执行一个外部进程,一种方法是让 build 执行一个外部工具(或脚本或批处理文件等)来跟踪时间并杀死自己。

A second way to do this from within Sublime would be to use a custom build target.在 Sublime 中执行此操作的第二种方法是使用自定义构建目标。 This is a command provided via a plugin that you tell Sublime to use in place of the command that it would normally use to execute the build.这是一个通过插件提供的命令,你告诉 Sublime 使用它来代替它通常用来执行构建的命令。

An example of a plugin that does something like this is the below.下面是执行此类操作的插件示例。 See this video on how to use plugins if you're unfamiliar with using a custom plugin in Sublime:如果您不熟悉在 Sublime 中使用自定义插件,请参阅此视频,了解如何使用插件

import sublime
import sublime_plugin

from Default.exec import ExecCommand


class TimeoutExecCommand(ExecCommand):
    """
    Drop in replacement for the exec command in Sublime Text build 3210/3211.
    """
    def run(self, **kwargs):
        self.timeout = kwargs.pop("timeout", 0)

        super().run(**kwargs)

        if self.timeout:
            sublime.set_timeout(self.time_out_build, self.timeout * 1000)

    def time_out_build(self):
        if self.proc:
            self.append_string(self.proc, "[Timeout exceeded: %.1f]" % self.timeout)
            self.proc.kill()
            self.proc = None

This implements a new timeout_exec command that can be used in builds to have them time out if they don't complete within a specific period of time.这实现了一个新的timeout_exec命令,如果它们没有在特定的时间段内完成,可以在构建中使用它来让它们超时。 As noted in the comment, requires one of the more recent Sublime Text 3 builds (it will not work in the 4xxx builds since those builds have an enhanced exec command).如评论中所述,需要更新的 Sublime Text 3 版本之一(它不适用于 4xxx 版本,因为这些版本具有增强的exec命令)。

To use this, you need to add some extra keys to your sublime-build file.要使用它,你需要在你的sublime-build文件中添加一些额外的键。 An example of a build file that uses this is:使用它的构建文件的示例是:

{
    "target": "timeout_exec",
    "cancel": {"kill": true},

    "timeout": 4,

    "shell_cmd": "echo \"Start\" && sleep 10 && echo \"Done\""
}

The target key tells Sublime to use this new command instead of the default exec command and the cancel key tells Sublime what extra arguments to provide if you try to cancel the build manually (ie from the menu, command palette, etc). target键告诉 Sublime 使用这个新命令而不是默认的exec命令,而cancel键告诉 Sublime 如果您尝试手动取消构建(即从菜单、命令面板等),需要提供什么额外的 arguments。

The command also implements a new build parameter of timeout that is the time in seconds after which the build will cancel itself.该命令还实现了一个新的timeout构建参数,即构建将自行取消的时间(以秒为单位)。 If the build is still running when the timeout is hit, it will cancel:如果超时时构建仍在运行,它将取消:

Start
[Timeout exceeded: 4.0]

Leaving out the timeout field or setting it's value to 0 disables the timeout, in which case the command behaves exactly as a build normally would.省略timeout字段或将其值设置为0会禁用超时,在这种情况下,命令的行为与构建通常会完全相同。

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

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