You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
locallgi=require'lgi'localGio=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.localfunctionrunPool(func, taskcnt, ...)
localargs= {...}
localresult, err=truelocalapp=Gio.Application.new(nil, {})
-- Use protected call for proper error handlinglocalfunctionrunner(...)
localr, e=xpcall(func, debug.traceback, ...)
-- Report first errorifnotrandresultthenresult, err=r, eendapp:release()
endfunctionapp:on_activate()
fori=1, taskcntdoapp:hold()
Gio.Async.start(runner)(i, table.unpack(args))
endendapp:run()
returnresult, errendlocalfunctiondoTest(round)
assert(runPool(function(number, arg)
locallen=math.random(1, 6)
print(('Waiting %d seconds in thread %d (round %d)'):format(len, number, arg))
localproc=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))
endfori=1,3doprint(('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.
The text was updated successfully, but these errors were encountered:
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:
runPool
seems VERY useful for anyone wanting to add parallelism into their scripts using lgi. I suggest integrating it intolgi
possibly asGio.Async.pool
.Known limitations:
xpcall
there yetunpack
instead oftable.unpack
(this seems to be already solved in lgi by usinglocal unpack = unpack or table.unpack
)The text was updated successfully, but these errors were encountered: