| |||||||||||
| |||||||||||
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)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 17:21:09) Associated topics are indexed under U114 - Lua - Threading and Coroutines.
Some other Articles
Lua Course - here is the indexDefensive coding techniques in PHP? July to December 2008 - Open Source training schedule Towards Object Oriented Programming in Lua Co-routines in Lua - co-operative processing Conversion of c/r line ends to l/f line ends Some sideways thoughts on the news cannot restore segment prot after reloc message - PHP / httpd Virtual Hosting on Apache httpd - a primer example FTP - how not to corrupt data (binary v ascii) 1781 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 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - 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). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||