wahern / cqueues

Continuation Queues: Embeddable asynchronous networking, threading, and notification framework for Lua on Unix.
http://25thandclement.com/~william/projects/cqueues.html
MIT License
244 stars 37 forks source link

Nesting controllers / parent discovery #245

Closed Oozlum closed 3 years ago

Oozlum commented 3 years ago

What I am trying to achieve is to load a user-defined script as a wrapped function, and for that script to be able to use queues itself transparently (i.e. without introducing additional framework into the script's environment):

-- main.lua
local cq = require'cqueues'
local ctrl = cq.new()

ctrl:wrap(loadfile('script.lua'))

-- call ctrl:step() in event loop
-- script.lua
local cq = require'cqueues'

controller:wrap(function()
  cq.sleep(2)
  print('Finished')
end)

The issue is what is controller? The way I see it, the script either has be able to do something like:

cq.get_my_parent_controller():wrap(...)

or:

local my_ctrl = cq.new()
my_ctrl:wrap(...)
-- how do I ensure my_ctrl:step() is called as part of the event loop?

What is the correct (recommended) way to achieve this?

daurnimator commented 3 years ago

What is the correct (recommended) way to achieve this?

cqueues.running()

However a better pattern is that your code snippets just look like linear code: if they need to create their own threads they can create their own cqueues controller.

Oozlum commented 3 years ago

Great, thank you. Meta question: how did you add the 'question' label? I tried to do it when I created this issue, but couldn't see an option?