Unable to get OS.execute() result unless blocking

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bugdot
OS.execute(str(userdir) + "/youtube-dl",  ["--dump-single-json", "ytsearc7:%s"%[term]], true, output)

The code prints output only if blocking is set to true, else it prints empty list[]. However if the execute is set to blocking it doesn’t result anything.

I want to have something like a signal that runs after the non-blocking OS.execute

Of course, this way of calling a subprocess won’t give you a result immediately if you don’t block. Not blocking means it’s fire-and-forget then…

That sounds like a feature request, which should be asked on Github.

An alternative would be to do this in a thread.

Zylann | 2018-05-15 13:02

:bust_in_silhouette: Reply From: Bugdot

The only method to get output while having it as non-blocking is to use threads.

This is the code I’m using to do that.

I set OS.exectute to blocking and then call it in a thread.

However I have a new problem. I need to kill the thread when needed.


func print_search(term):
	search_ongoing = true
	search_completed = false
	print("Search starting for "+ term)
	OS.execute(str(userdir) + "/youtube-dl",  ["--dump-single-json", "ytsearch1:"+term], true, output)
	search_ongoing = false
	search_completed = true
	search_read = false
	print("Search finished")
	$StatusLabel.text="Search finished"

func _on_Search_pressed():
	if search_ongoing == false:
		search_thread = Thread.new()
		var search_err = search_thread.start(self, "print_search", [$UISearch/LineEdit.text])

Damn, have you find a solution to this? I wish we could poll the data from the output. I’m creating an AppManager and want to log the stdout data in a RichTextLabel :open_mouth:

For example, “crystal test.cr”, could be handled in one thread, and “crystal test2.cr” could be handled in another. (just an example, let’s say the crystal app is logging something every xx seconds). Would be cool to read from stdout and display the data in a RTL node.

wombatTurkey | 2019-10-05 12:50