Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Co-routines in Lua - co-operative processing
CoRoutines in Lua allow you to have a number of pieces of co-operative code which (whilst they •re not running in parallel in a true multithreaded way) allow you to progress through multiple strands of your code at the sametime.
Let •s take an example. I have a rather large text file that lists out all the railway stations in the British Isles, together with the number of passengers joining the railway and leaving the railway there, and various other miscellaneous bits of data, and I want to iterate through the data processing it.
The data format is rather ugly - sometimes the numbers are quoted and sometimes not, and there are commas in the numbers over 999 too. Spaces and tabs mean different things, and zero is actually represented by "-". I would love to write code that •s a simple loop that says "get station" as a function call, which would read from the file and process and return the next line ... but there are all sorts of issues with static variables that would need to be considered.
Lua •s "coroutine"s can be created, then resumed (until they yield) time and again, so that in effect using them is like walking up to a coffee machine and pressing the button for the next cup ... which will keep coming while there •s coffee in the machine.
Here •s a snippet of code to call a coroutine ...
instream = coroutine.create(recordfeeder)
while coroutine.status(instream) ~= "dead" do
if fields then
-- process those fields
Yes, it does look very similar to Java •s iterators, or Python •s generators.
So why don •t I loop through all the records in my incoming file and produce a table of station objects, then write another loop to handle them? Because I don •t really want or need to have 2520 station objects or tables from Waterloo (88,219,856 passengers per year) to Tyndrum Lower (17 passengers in a year) in memory after reading through my data - I want to filter on the fly. Rather than filling a reservoir with water (and having to build it big enough) then draining all the water back out, I prefer to have a pipe with a tap which I can turn on when I need the next record.
The Complete source code for the example I'm talking about here is available here on our web site (written 2008-06-29)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesU114 - Lua - Threading and Coroutines. 
Embedding Lua to perform tailored code at an interval - (2014-05-03) 
Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17) 
Lua examples - coroutines, error handling, objects, etc - (2009-10-15) 
Passing parameters to a coroutine in Lua - (2009-08-01) 
What to do with a huge crop of apples - (2008-11-04) 
If you are learning Lua, here are some more examples - (2008-07-06)
Some other Articles
Lua Course - here is the indexDefensive coding techniques in PHP?July to December 2008 - Open Source training scheduleTowards Object Oriented Programming in LuaCo-routines in Lua - co-operative processingConversion of c/r line ends to l/f line endsSome sideways thoughts on the newscannot restore segment prot after reloc message - PHP / httpdVirtual Hosting on Apache httpd - a primer exampleFTP - how not to corrupt data (binary v ascii)
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).