0 votes

I want to get, specifically, the output of OS.execute("ping", ["1.1.1.1", "-n", "1"], true, output) without blocking the main thread.

The solution, I believe, would be something to do with threading, but I am not very familiar with threads and I do not know the proper way to implement a "callback" where the result of the above command would be sent to the main thread to then be processed and the side thread killed.

Godot version 3.5
in Engine by (27 points)

2 Answers

0 votes
Best answer

For those curious, this code snippet is what helped me solve this, from "youwin"

The thread.wait_to_finish() returns the return value of the thread.

extends Node2D

var thread: Thread

func _ready() -> void:
    thread = Thread.new()
    thread.start(self, "_thread")

    while thread.is_alive():
        yield(get_tree(), "idle_frame")

    print(thread.wait_to_finish())
    thread = null

func _thread() -> String:
    var output := []
    var err: int = OS.execute("CMD.exe", ["/C", "dir"], true, output)

    if err != 0:
        printerr("Error occurred: %d" % err)

    return PoolStringArray(output).join("\n")
by (27 points)
0 votes

Quite simple actually ~ the third argument just needs to be false
OS.execute("ping", ["1.1.1.1", "-n", "1"], false, output)

Here's the docs :)

by (14 points)

This results in output not being set

Per the docs, the output param will be empty if blocking is false (which makes sense).

Have you tried piping the output of ping into a file and monitoring that file from another script?

Would have issues from the file being locked/actively written from the other thread

If you read the file while it's being written to the data could be corrupted

I found this gdnative plugin, seems to solve your problem but I haven't tested it. https://github.com/odedstr/Godot-std_io

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.