Skip to content

Commit 8b34dcf

Browse files
committed
Added timeout functionality
1 parent cb7f04d commit 8b34dcf

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Here is the example of default ```Languages.sublime-settings```.
4747
},
4848
"lang-cpp" : {
4949
"compile_cmd" : "g++ \"${code_file}\" -o \"${code_file_path}/${code_file_base_name}\"",
50-
"execute_cmd" : "\"${code_file_path}/${code_file_base_name}\""
50+
"execute_cmd" : "\"${code_file_path}/${code_file_base_name}\"",
51+
"timeout" : 3
5152
},
5253
"lang-java" : {
5354
"compile_cmd" : "javac \"${code_file}\"",
@@ -59,6 +60,8 @@ Here is the example of default ```Languages.sublime-settings```.
5960
}
6061
~~~
6162

63+
```timeout``` here specifies how long process should be allowed to run. Default is 3 seconds.
64+
6265
The key ```lang-extension``` is used to determine which language command should be used.
6366
Following variables are available :
6467
* ${code_file_extension} : Extension of file being executed.

languages.sublime-settings

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"lang-c" : {
33
"compile_cmd" : "gcc \"${code_file}\" -o \"${code_file_path}/${code_file_base_name}\"",
4-
"execute_cmd" : "\"${code_file_path}/${code_file_base_name}\""
4+
"execute_cmd" : "\"${code_file_path}/${code_file_base_name}\"",
55
},
66
"lang-cpp" : {
77
"compile_cmd" : "g++ \"${code_file}\" -o \"${code_file_path}/${code_file_base_name}\"",
8-
"execute_cmd" : "\"${code_file_path}/${code_file_base_name}\""
8+
"execute_cmd" : "\"${code_file_path}/${code_file_base_name}\"",
9+
"timeout" : 3
910
},
1011
"lang-java" : {
1112
"compile_cmd" : "javac \"${code_file}\"",

run.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
settings = sublime.load_settings("Languages.sublime-settings")
1414

15+
DEFAULT_TIMEOUT = 3
16+
1517

1618
class Environment(object):
1719
""" Environment for target file.
@@ -113,12 +115,26 @@ def execute(input_view, output_view):
113115
stderr=subprocess.STDOUT,
114116
universal_newlines=True
115117
)
116-
output, errors = run.communicate(Environment.last_input_view.substr(sublime.Region(0, input_view.size())))
117-
run.wait();
118-
output_view.run_command("output_file_edit", {"append": run.args + "\n\n"});
119-
output_view.run_command("output_file_edit", {"append": "------------------------------------------\n"});
120-
output_view.run_command("output_file_edit", {"append": output + "\n"});
121-
output_view.run_command("output_file_edit", {"append": "------------------------------------------\n"});
118+
output_view.run_command("output_file_edit", {"append": run.args + "\n\n"});
119+
output = None
120+
timeout = DEFAULT_TIMEOUT
121+
if "timeout" in lang_settings:
122+
timeout = lang_settings["timeout"]
123+
124+
try:
125+
output, errors = run.communicate(Environment.last_input_view.substr(sublime.Region(0, input_view.size())), timeout=timeout)
126+
except subprocess.TimeoutExpired:
127+
output_view.run_command("output_file_edit", {"append": "Timeout expired of " + str(timeout) + " seconds\n"});
128+
output_view.run_command("output_file_edit", {"append": "Timeout is added to stop running the process indefinitely, It can be changed in Languages.sublime-settings\n"});
129+
run.kill()
130+
output, errors = run.communicate()
131+
finally:
132+
if output != None:
133+
output_view.run_command("output_file_edit", {"append": "------------------------------------------\n"});
134+
output_view.run_command("output_file_edit", {"append": output + "\n"});
135+
output_view.run_command("output_file_edit", {"append": "------------------------------------------\n"});
136+
137+
122138
output_view.run_command("output_file_edit",
123139
{"append": "Execution finished with exit code : " + str(run.returncode)})
124140

0 commit comments

Comments
 (0)