|
Hi, I'm trying to wrap my head around how to use coroutines properly. A function in my program iterates over a directory and inserts all found files into a table. Next, I need to do some stuff with this table, and iterate over it to insert the files into an SQL database. --FUNCTIONAL NON-COROUTINE VERSION local D,P=Database,ProgressBar local Files=IterateDir() --returns the table if D:transaction() then --prepare database for transaction P:setMaximum(#Files) --set the progressbar maximum value to the amount of files in the table for FileName,Extension in pairs(Files)do D:exec('insert into Files("'..Filename..'","'..Extension..'")') --execute SQL query P:setValue(ProgressBar:value()+1) --step the progressbar end if D:commit() then --commit the database print'Operation finished, database updated succesfully' P:reset() --reset the progressbar end end Using some examples I've found on the internet, I'm trying to wrap the loop in a coroutine, however I'm not quite sure how to implement coroutine.yield() in this case (hence Lua returns the error 'cannot resume dead coroutine') --DYSFUNCTIONAL COROUTINE VERSION local D,P=Database,ProgressBar local Files=IterateDir() --returns the table local Routine=coroutine.wrap(function(FileName,Extension) D:exec('insert into Files("'..Filename..'","'..Extension..'")') --execute SQL query P:setValue(ProgressBar:value()+1) --step the progressbar coroutine.yield() end) if D:transaction() then --prepare database for transaction P:setMaximum(#Files) --set the progressbar maximum value to the amount of files in the table for FileName,Exension in pairs(Files)do Routine(File,Extension)end if D:commit() then --commit the database print'Operation finished, database updated succesfully' P:reset() --reset the progressbar end end Can somebody help me get the second version working? Ultimately, I would like to use true OS threads but as I understand it I need to wait until LuaJIT FFI supports callbacks, or I need to use an additional module (I would like to keep dependencies to a minimum) Thanks in advance Ralf |