functiondeal(list) local p = 0 localfunctionfoo() local from, to = p + 1, math.min(p + 50, #list) for i = from, to do local id = list[i] dosth(id) end p = to if p < #list then timer:start_once(1, foo) end end foo() end
counts = {} max_counts = {} functiontry_yield() local co = coroutine.running() assert(co ~= nil) counts[co] = counts[co] + 1 if counts[co] >= max_counts[co] then counts[co] = 0 coroutine.yield() end end
functionwith_coroutine(f, n, t) returnfunction(...) local co = coroutine.create(f) counts[co] = 0 max_counts[co] = n
localfunctionfoo(...) coroutine.resume(co, ...) ifcoroutine.status(co) == 'dead'then counts[co] = nil max_counts[co] = nil return end timer:start_once(t, foo) end foo(...) end end