Add run_async to shell, which should be more stable than the previous threading

This might fix the "random crash" problem?
This commit is contained in:
blinry 2020-11-02 13:13:04 +01:00
parent 53f60247f3
commit e220211140
4 changed files with 67 additions and 40 deletions
scenes

View file

@ -18,8 +18,30 @@ func cd(dir):
# Run a shell command given as a string. Run this if you're interested in the
# output of the command.
func run(command, crash_on_fail=true):
var shell_command = ShellCommand.new()
shell_command.command = command
shell_command.crash_on_fail = crash_on_fail
run_async_thread(shell_command)
exit_code = shell_command.exit_code
return shell_command.output
func run_async(command, crash_on_fail=true):
var shell_command = ShellCommand.new()
shell_command.command = command
shell_command.crash_on_fail = crash_on_fail
var _thread = Thread.new()
_thread.start(self, "run_async_thread", shell_command)
return shell_command
func run_async_thread(shell_command):
var debug = false
var command = shell_command.command
var crash_on_fail = shell_command.crash_on_fail
if debug:
print("$ %s" % command)
@ -64,8 +86,9 @@ func run(command, crash_on_fail=true):
if debug:
print(result["output"])
exit_code = result["exit_code"]
return result["output"]
shell_command.output = result["output"]
shell_command.exit_code = result["exit_code"]
shell_command.emit_signal("done")
func _shell_binary():
if _os == "X11" or _os == "OSX":
@ -75,25 +98,25 @@ func _shell_binary():
else:
helpers.crash("Unsupported OS: %s" % _os)
var _t
func run_async(command):
_t = Thread.new()
_t.start(self, "run_async_thread", command)
func run_async_thread(command):
var port = 1000 + (randi() % 1000)
var s = TCP_Server.new()
s.listen(port)
var _pid = OS.execute("ncat", ["127.0.0.1", str(port), "-c", command], false, [], true)
while not s.is_connection_available():
pass
var c = s.take_connection()
while c.get_status() == StreamPeerTCP.STATUS_CONNECTED:
read_from(c)
OS.delay_msec(1000/30)
read_from(c)
c.disconnect_from_host()
s.stop()
#var _t
#func run_async(command):
# _t = Thread.new()
# _t.start(self, "run_async_thread", command)
#
#func run_async_thread(command):
# var port = 1000 + (randi() % 1000)
# var s = TCP_Server.new()
# s.listen(port)
# var _pid = OS.execute("ncat", ["127.0.0.1", str(port), "-c", command], false, [], true)
# while not s.is_connection_available():
# pass
# var c = s.take_connection()
# while c.get_status() == StreamPeerTCP.STATUS_CONNECTED:
# read_from(c)
# OS.delay_msec(1000/30)
# read_from(c)
# c.disconnect_from_host()
# s.stop()
func read_from(c):
var total_available = c.get_available_bytes()