Skip to content
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

Suggestion: add runPool functionality to lgi #240

Open
v1993 opened this issue Mar 6, 2020 · 0 comments
Open

Suggestion: add runPool functionality to lgi #240

v1993 opened this issue Mar 6, 2020 · 0 comments

Comments

@v1993
Copy link
Contributor

v1993 commented Mar 6, 2020

I often use lgi inside various very linear scripts where idea of main loop is plain unacceptable. However, today I ran into problem: I need to run few actions in parallel, while keeping overall flow of script linear.

Here's my solution together with a simple test:

local lgi = require 'lgi'
local Gio = lgi.Gio

-- Run taskcnt instances of func as async tasks and wait until all
-- of them finish. Function is called with task ID and
-- additional arguments you passed to this call.

local function runPool(func, taskcnt, ...)
	local args = {...}
	local result, err = true
	local app = Gio.Application.new(nil, {})

	-- Use protected call for proper error handling
	local function runner(...)
		local r, e = xpcall(func, debug.traceback, ...)

		-- Report first error
		if not r and result then
			result, err = r, e
		end

		app:release()
	end

	function app:on_activate()
		for i=1, taskcnt do
			app:hold()
			Gio.Async.start(runner)(i, table.unpack(args))
		end
	end

	app:run()

	return result, err
end

local function doTest(round)
	assert(runPool(function(number, arg)
		local len = math.random(1, 6)
		print(('Waiting %d seconds in thread %d (round %d)'):format(len, number, arg))
		local proc = assert(Gio.Subprocess.new({'sleep', len}, 'NONE'))
		proc:async_wait()
		print(('Finished waiting %d seconds in thread %d (round %d)'):format(len, number, arg))
	end, 4, round))
end

for i=1,3 do
	print(('Testing round %d start'):format(i))
	doTest(i)
	print(('Testing round %d end'):format(i))
end

runPool seems VERY useful for anyone wanting to add parallelism into their scripts using lgi. I suggest integrating it into lgi possibly as Gio.Async.pool.

Known limitations:

  • Require tweaking to work in Lua 5.1
    • Error handling will not work as you can't yield across xpcall there yet
    • Use unpack instead of table.unpack (this seems to be already solved in lgi by using local unpack = unpack or table.unpack)
  • Reports only first error. Seems reasonable overall, but changes to this behavior can be done if you think they should be.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant