-
Notifications
You must be signed in to change notification settings - Fork 189
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
Luv Async not performing as expected #629
Comments
So, this is mostly just an artifact of how libuv's async_t works. Async callbacks are called on the event loop. This leads to a few different things: The description of
If the event loop is sleeping (like waiting for io), async_send will cause a tick so that the callback can be called nearly immediately (on the tick). An AddendumIf you're looking for a function to run completely separately (on a different thread), then you can use either:
Just be aware that nearly all lua implementations (lua-lanes notwithstanding) are not re-entrant, so you can only pass |
I had a feeling that is what async was doing, the first handful of points I knew. I didn't know async prevents Spawning a OS thread is an option though IMO a very expensive one. I will investigate the threadpool stuff :) The documentation for |
Its a bit more complicated and involved.
Using this you could create a bit of code that yields the current coroutine when you queue the work and then resume it in the after_work_callback. |
That makes sense to me. I appreciate you! I will close this out as I have the answers I am looking for. |
So I hate to be that person who re-opens issues, I thought I had what I needed with our conversation earlier. However, after dinner and testing this a bit, I am running into very similar behavior to what I was seeing with the My code local luv = require("luv")
local func1 = function(params)
print(params)
local max_loop = 1000000000
local _ = 0
while _ < max_loop do _ = _ + 1 end
return 'dun'
end
local func2 = function(params)
print(params)
local max_loop = 1000
local _ = 0
while _ < max_loop do _ = _ + 1 end
return 'dun'
end
local func1work = luv.new_work(func1, function(result) print(string.format("func1 complete! %s", result)) end)
local func2work = luv.new_work(func2, function(result) print(string.format("func2 complete! %s", result)) end)
print("Starting background operations")
func1work:queue("hello")
func2work:queue("world")
print("Running long operation in foreground while we wait")
local max_loop = 1000
local _ = 0
luv.run()
while _ < max_loop do
_ = _ + 1
end
print("Completed foreground operation") My expected output
Actual output
It seems that the |
Sorry if this wasn't clear: This is mostly just a limitation of event loops. To call a callback, the event loop must be running. You should basically never be writing code after |
Ahh that makes sense! In this case, I am testing something that will (eventually) be running in a program that manages the libuv event loop for me, so with that being said, it sounds like I may not have to worry about that. To avoid prematurely closing this out (again), I will report back once I have tested that in said program to ensure that it does indeed work without me having to worry about Edit: Tested and indeed you are correct! My above issue is due to me managing the libuv event loop, and since the program I am writing for manages it for me, this works perfectly. Thank you! |
Hello! This is likely due simply to a lack of my understanding of how async is expected to work in luv, but I have the below code that appears to be running synchronously, while I would expect it to run synchronously. I don't know what I am missing and the docs are a bit light on how to use the async handles effectively. The code
With this code, I would expect the print output to look something like
As
async1
is a very long running dry loop. However, this doesn't seem to be the case, instead this completes in order of async calls. IE,async1
completes first, thenasync2
then the foreground task. I am not quite sure what I am doing wrong here. Below is my "test" output as it stands right now. Thoughts?The text was updated successfully, but these errors were encountered: