-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
evolution of grab_stdout
to grab_all
#25
Comments
This is very nice. I don't see an obvious way to "improve" it. There's something that caught my attention, tough. I've thought about streaming into The sockets are messy, and the selector api is overcomplicated :( |
Thank you! In my code I use
Actually for 2. I tested with the following code: sched.run(function()
local sktd, pid = selector.grab_all('ping -c 5 8.8.8.8', handles)
local a = handles.ret_r.handler:read()
if a ~= nil then
print("return code: "..a)
end
local start = sched.get_time()
local method = "non-blocking"
local attempts = 1
if method == "non-blocking" then
while true do
local _, state, code = nixio.waitpid(pid, "nohang")
if state == "exited" then break end
attempts = attempts + 1
sched.sleep(0.001)
end
elseif method == "blocking" then
local state, code = nixio.waitpid(pid)
end
local time_taken = sched.get_time() - start
print(attempts, "attempts were required at waitpid, took ",time_taken, "seconds" )
print("function completed!")
end) a typical result for
and a typical result for
These times varied around 0.004 seconds for each approach. So the non-blocking approach might well be worth doing! Re the Selector API, as I gain more experience with it I'll see if I can make any suggestions. As I've mentioned before, when I have time I'm studying the Snabb implementation of concurrent ML in Lua (#23 (comment)). This library integrates with Only my spidey senses tingling at the moment but I suspect it would be really nice to maybe build a task/activity based multitasking system like Lumen (which I find immensely intuitive) on the basis of CML operations. Something that avoids the inherent ugliness of coloured functions (async/sync versions of everything) but can build more natural structured concurrency. |
A quick note, is the waitpid in the main program neccesary? Can't you detect the 'closed' error when reading from the stream? |
We need to waitpid in the main program to avoid zombie processes hanging around eating up process descriptors https://duyanghao.github.io/ways_avoid_zombie_process/ |
Oh I see. The fact that the library's user has call nixio.waitpid outside grab_all, in the main program, is not very safe/elegant. |
Hi Xopxe
There are a few things about the
selector.grab_stdout
tool that don't work for me:stderr
which many tools such aswget
use extensivelywaitpid
must be called on child processes otherwise they become zombie processes)I've put together a demo of what a more comprehensive function might look like. Some things to note:
nixio.waitpid()
on the grandchild's pid. Once this returns it sends the return code (the marker of success or failure) on a stream, much like thestdout
andstderr
streamsgrab_stdout
to use a function like this under the hoodOkay the function looks like this:
An example of how it works is like this:
This is very much a first attempt. Would love your input. I'm sure this could be made more elegant (through functional composition perhaps to execute the double fork and waiting on the grandchild pid), but it works well and allows for efficient execution of external commands (no need to poll on the pid to ascertain program completion). Would love your thoughts on this before I submit a PR.
Also a very cool addition would be to bind
stdin
to this process, perhaps through a stream as well so that we can take full input/output command of external programs. I tried extending this just by adding another handler specified the same as the other 3 but just managed to achieve 100% CPU utilisation, which is no fun - I suspect you understand posix sockets much better than me :)The text was updated successfully, but these errors were encountered: