Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Our telephone lines have been repaired - sorry if you couldn't get through
+44 (0) 1225 708225 / 01225 708225
Formally open 9 a.m. to 5 p.m. (but usually around much longer

Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, 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 •[99]re not running in parallel in a true multithreaded way) allow you to progress through multiple strands of your code at the sametime.

Let •[99]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 •[99]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 •[99]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 •[99]s coffee in the machine.

Here •[99]s a snippet of code to call a coroutine ...

instream = coroutine.create(recordfeeder)
while coroutine.status(instream) ~= "dead" do
  coroutine.resume(instream)
  if fields then
    -- process those fields
    end
  end


Yes, it does look very similar to Java •[99]s iterators, or Python •[99]s generators.

So why don •[99]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 •[99]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 articles
U114 - Lua - Threading and Coroutines.
  [4270] Embedding Lua to perform tailored code at an interval - (2014-05-03)
  [3395] Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17)
  [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
  [2314] Passing parameters to a coroutine in Lua - (2009-08-01)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [1699] If you are learning Lua, here are some more examples - (2008-07-06)


Back to
Conversion of c/r line ends to l/f line ends
Previous and next
or
Horse's mouth home
Forward to
Towards Object Oriented Programming in Lua
Some other Articles
Lua Course - here is the index
Defensive 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)
4750 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 at 50 posts per page


This 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1691_Co- ... ssing.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat