LUA Learning Notes-Chapter 9.2-Pipelines and filters

Source: Internet
Author: User
Tags lua

1, Producer-consumer problem

A typical example of couroutine is the producer-consumer problem. Suppose there are two functions, a constant produce of some value (for example, read from one file), and the other continually consume the values (for example, to another file). The two functions should look like this:

function producer () while
	true does 
	local x = Io.read ()--Produce new value 
	send (x)--Send to Consumer 
	en D
End
function consumer () 
	while True does 
	local x = Receive ()--Receive from producer 
	Io.write (x, "\ n") )--Consume new value 
	end
End

Both functions are constantly executing, so the question is, how do you match send and recv? Who is the first one? Each of them has its own main loop.

Use Resume-yield to solve:

function receive (PROD)
    local status, Value = Coroutine.resume (prod)
    return value
end

function Send (x)
    Coroutine.yield (x)
end

function producer ()
    return coroutine.create (function () while
        true
            local x = Io.read ()--Produce new value (
            send (x)
        end
    )
end
  
function consumer (PROD) C15/>while true do
        local x = Receive (PROD) – Receive from producer
        Io.write (x, "\ n")--consume new value
    End
End

p = producer ()
consumer (p)

When a synergistic call to yield does not enter a new function, instead it returns a call to the pending resume. Similarly, calling resume does not start a new function but returns a call to yield.
This nature is exactly what we need and is consistent with the way in which send-receive work together. Receive wakes the producer to produce a new value, and send sends the resulting value to the consumer.

At the beginning of the call to the consumer, when the consumer needs value when he evokes the producer production value, the producer produces the value and stops until the consumer requests again. Call this design a consumer-driven design.

2. Increase the filter to process the intermediate data
Filter refers to the producer and consumer, you can do some conversion processing data. The filter is both producer and consumer at the same time, he requests the producer to produce the value and transfer the format to the consumer, modify the above code to add the filter.

First look at the logic before adding filter, basically is producer to send, send to consumer, consumer go recv, recv from producer. After adding the filter, because the filter needs to do some conversion to the data, so the logic is, producer go to the Send,send to filter, filter recv, recv from producer, filter Go send,send to consumer, consumer go recv, recv from filter.

The complete code is as follows:

function receive (PROD) local status, Value = Coroutine.resume (prod) return value End Function s
            End (x) Coroutine.yield (x) End Function producer () return Coroutine.create (function () while True
    Local x = Io.read ()--produce new value, send (x) end, End Function consumer (PROD)  While true does local x = Receive (PROD)--Receive from producer if X then Io.write (x, "\ n")--consume new                                                                                                              
    Value else break end End Function Filter (PROD) Return Coroutine.create (function () for line = 1, Math.hug e do local x = Receive (prod) x = String.Format ('%5d%s ', line, X)--formatted string,--X = String.form At ('%05d%s ', line, x)-output less than 5 bits are padded with 0 for send (x) end end) End p = producer () F = filter (p) consumer ( f) 

Read the example above, a bit like the pipe in UNIX. With pipe, each task is executed in its own process, with coroutine and each task executing in its own coroutine. The pipe provides a buffer between writer (producer) and Reader (consumer), so the relative speed of operation is quite acceptable. This is a very important feature of the pipe, because the communication between the process, the price is still a bit large. With Coroutine, the cost of switching between different tasks is much smaller, basically a function call, so writer and reader can almost be said to go hand in hand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.