Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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  [82][ac] Ä re not running in parallel in a true multithreaded way) allow you to progress through multiple strands of your code at the sametime.

Let  [82][ac] Ä 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  [82][ac] Ä 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  [82][ac] Ä 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  [82][ac] Ä s coffee in the machine.

Here  [82][ac] Ä 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  [82][ac] Ä s iterators, or Python  [82][ac] Ä s generators.

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

Back to
Conversion of c/r line ends to l/f line ends
Previous and next
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)
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 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
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: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb