Last active
April 10, 2017 00:32
-
-
Save neikeq/5b3ab0461733d1100561b0ba227a19e0 to your computer and use it in GitHub Desktop.
Example usage of the Process class from GDScript. Check below for example applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Node | |
| const BLOCKING = false | |
| const PRINTRAW = true | |
| onready var te = get_node("TextEdit") | |
| var process = Process.new() | |
| var timer = Timer.new() | |
| func _ready(): | |
| add_child(timer) | |
| process.set_cwd("/home/whatever") | |
| process.set_environment({ "HMM": "okthx" }, false) | |
| process.set_redirect_flags(Process.REDIRECT_ALL) # | Process.REDIRECT_STDERR_TO_STDOUT | |
| if not process.start("/home/whatever/testprocess", [ "a", "--b", "hello there!" ]): | |
| printerr("Failed to start process") | |
| return | |
| prints("Process PID: ", str(process.get_pid())) | |
| var input = [ | |
| "whatever\n", | |
| "<3\n", | |
| "a\n", | |
| "quit\n" | |
| ] | |
| for text in input: | |
| var data = text.to_utf8() | |
| if process.can_write(data.size()): | |
| process.write(data) | |
| else: | |
| print("Cannot write :(") # not running or buffer is full | |
| if BLOCKING: | |
| block() | |
| else: | |
| timer.set_wait_time(0.030) | |
| timer.connect("timeout", self, "_poll_process") | |
| timer.start() | |
| func print_raw_utf8(data): | |
| if te != null: | |
| te.set_text(te.get_text() + data.get_string_from_utf8()) | |
| elif PRINTRAW: | |
| # accurate output, but need to check terminal | |
| printraw(data.get_string_from_utf8()) | |
| else: | |
| # not accurate output since it adds new line | |
| print(data.get_string_from_utf8()) | |
| func read_all(channel): | |
| process.set_read_channel(channel) | |
| if process.get_available_bytes() > 0: | |
| return process.read_all() | |
| return PoolByteArray() | |
| func on_process_exit(): | |
| prints("Process exited.", OS.get_ticks_msec()) | |
| prints("Exit code:", process.get_exit_code()) | |
| if process.get_exit_status() == Process.EXIT_STATUS_CRASH: | |
| print("Process crashed") | |
| func block(): | |
| if process.get_state() != Process.STATE_NOT_RUNNING: | |
| process.wait_for_started(-1) | |
| assert(process.get_state() != Process.STATE_STARTING) # shouldn't timeout with -1 | |
| if process.get_state() == Process.STATE_RUNNING: | |
| prints("Process started.", OS.get_ticks_msec()) | |
| process.wait_for_finished(-1) | |
| assert(process.get_state() == Process.STATE_NOT_RUNNING) # shouldn't timeout with -1 | |
| on_process_exit() | |
| else: | |
| prints("exec failed", OS.get_ticks_msec()) # unix | |
| print("Output:") | |
| print_raw_utf8(read_all(Process.CHANNEL_STDOUT)) | |
| print_raw_utf8(read_all(Process.CHANNEL_STDERR)) | |
| func _poll_process(): | |
| process.poll() | |
| if process.get_state() == Process.STATE_RUNNING: | |
| print_raw_utf8(read_all(Process.CHANNEL_STDOUT)) | |
| print_raw_utf8(read_all(Process.CHANNEL_STDERR)) | |
| elif process.get_state() == Process.STATE_NOT_RUNNING: | |
| timer.stop() | |
| on_process_exit() | |
| # remaining output | |
| print_raw_utf8(read_all(Process.CHANNEL_STDOUT)) | |
| print_raw_utf8(read_all(Process.CHANNEL_STDERR)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio; | |
| import std.file; | |
| import std.algorithm; | |
| import core.thread; | |
| import std.process; | |
| int main(string[] args) | |
| { | |
| writeln("Working directory: " ~ getcwd()); | |
| //stdout.flush(); | |
| writeln("Arguments:"); | |
| //stdout.flush(); | |
| string[string] env = environment.toAA(); | |
| writeln(env); | |
| //stdout.flush(); | |
| foreach (string arg; args) { | |
| writeln(arg); | |
| //stdout.flush(); | |
| } | |
| //Thread.sleep(dur!"seconds"(5)); | |
| writeln("Infinite input loop (until you type q or quit):"); | |
| //stdout.flush(); | |
| while (true) { | |
| string line = readln(); | |
| if (line is null) | |
| continue; | |
| if (line.endsWith("\n")) { | |
| line = line[0..$-1]; | |
| } | |
| if (line == "quit" || line == "q") { | |
| //writeln("Quit!"); | |
| //stdout.flush(); | |
| break; | |
| } | |
| foreach (char elem; line) { | |
| writef("%x\n", elem); | |
| //stdout.flush(); | |
| } | |
| stderr.writeln("Input: " ~ line); | |
| stderr.writeln("Input: " ~ line[1..$]); | |
| } | |
| //Thread.sleep(dur!"seconds"(5)); | |
| writeln("Finished!"); | |
| //stdout.flush(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import os | |
| import time | |
| print 'Working directory: ' + os.getcwd() | |
| #sys.stdout.flush() | |
| print 'Arguments:' | |
| #sys.stdout.flush() | |
| print(os.environ) | |
| #sys.stdout.flush() | |
| for arg in sys.argv: | |
| print arg | |
| #sys.stdout.flush() | |
| print 'Infinite input loop (until you type q or quit):' | |
| sys.stdout.flush() | |
| #time.sleep(5) | |
| while True: | |
| line = raw_input() | |
| if line.endswith('\n'): | |
| line = line[:-1] | |
| if line == 'quit' or line == 'q': | |
| #print 'Quit!' | |
| #sys.stdout.flush() | |
| break | |
| if line: | |
| for elem in line: | |
| print elem.encode("hex") | |
| print "Input: " + line | |
| print "Input: " + line[1:] | |
| sys.stdout.flush() | |
| #time.sleep(5) | |
| print 'Finished!' | |
| sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment