lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

After some (rather murky) thinking, I believe that the natural behavior for a "task" going out of scope is to be killed right away.

So I think I'll make all library objects __toclose-able in this meaning, make sure they are always held weakly by the library, and see how it looks.


On 23/9/22 03:08, Sean Conner wrote:
It was thus said that the Great John Belmonte once stated:
On Tue, Sep 20, 2022 at 10:21 PM Jorge Visca <> wrote:

I wonder how would you handle waiting on several
First, it needs cancellation (coming in part 3).

Given that, I usually implement a `wait_any()` utility.  The inputs can be
any function that uses return of control to signal the event.  (For
example, the async_wait() method of Event.)  Then:

-- run these functions concurrently; when one returns, cancel the others
   I've been playing around with my own implementation of this for [1].  I got cancellation working (using coroutine.close()),
so the following bit of code works as intended:

	local tcp     = require ""
	local nursery = require "nursery"

	local function get_quote(list)
	  local conn = tcp.connect('','qotd')
	  if conn then
	    for line in conn:lines() do
	-- get_gopher and get_gemini not shown, but are similar
	-- in nature.

	local data_quote  = {} -- used to accumulate the data
	local data_gopher = {} -- from each of these services
	local data_gemini = {}

	  local _ <close> = nursery(1) -- I don't care about the name
	        :spawn(get_gemini,data_gemini)	-- so '_' is it!

   The nursery:spawn() function returns the nursery so calls can be chained
like this.  The call nursery(1) sets up a timeout for the entire nursery
collection and will cancel any current jobs still running in the nursery.
You can also write it as:

	nursery() -- no timeout specified, so take as long as you like
		:wait() -- this is also the __close() metamethod

   I also support a wait_any(), which will wait until one of the jobs
finishes, then cancel the others:


   I'm awaiting futher installments of the series, as I can't follow the
documentation for Python Trio, since I don't do Python.


[1]	My event/coroutine driver: