Tuesday, 18 March 2014
Asynchronous download using Coroutine - Lua
require("socket")
function download()
local host, file, port = "www.w3.org", "/TR/REC-html32.html", 80
local connection = assert(socket.connect(host, port))
local count = 0 -- counts number of bytes read
connection:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
while true do
local response, status = receive(connection)
if(response ~= nill) then count = count + string.len(response) end
--print(response)
if status == "closed" then break end
end
connection:close()
print(file, count)
end
--[[function receive(connection)
connection:settimeout(0)
return connection:receive(2^10)
end]]--
function receive(connection)
connection:settimeout(0) -- do not block
local response, status = connection:receive(2^10)
if status == "timeout" then
coroutine.yield(connection)
end
return response, status
end
threads = {} -- list of all live threads
function get ()
-- create coroutine
local co = coroutine.create(function () download() end)
-- insert it in the list
table.insert(threads, co)
end
function dispatcher()
while true do
local count = table.getn(threads)
if(count <= 0) then break end
for i = 1, count do
local status, ret = coroutine.resume(threads[i])
if not ret then table.remove(threads, i) break end
end
end
end
get()
get()
get()
get()
get()
dispatcher()
function download()
local host, file, port = "www.w3.org", "/TR/REC-html32.html", 80
local connection = assert(socket.connect(host, port))
local count = 0 -- counts number of bytes read
connection:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
while true do
local response, status = receive(connection)
if(response ~= nill) then count = count + string.len(response) end
--print(response)
if status == "closed" then break end
end
connection:close()
print(file, count)
end
--[[function receive(connection)
connection:settimeout(0)
return connection:receive(2^10)
end]]--
function receive(connection)
connection:settimeout(0) -- do not block
local response, status = connection:receive(2^10)
if status == "timeout" then
coroutine.yield(connection)
end
return response, status
end
threads = {} -- list of all live threads
function get ()
-- create coroutine
local co = coroutine.create(function () download() end)
-- insert it in the list
table.insert(threads, co)
end
function dispatcher()
while true do
local count = table.getn(threads)
if(count <= 0) then break end
for i = 1, count do
local status, ret = coroutine.resume(threads[i])
if not ret then table.remove(threads, i) break end
end
end
end
get()
get()
get()
get()
get()
dispatcher()
Tuesday, 4 March 2014
Permutations using Coroutines in Lua
First let's look at the algorithm for Permutations using recursion
algorithm Permutation(array, count)
if array index is 0, display the array
for each item in array until count
swap last and first item
Permutation(array, count -1)
swap last and first item
end
end
now the code
function permuterate (a, n)
if n == 0 then
--printResult(a)
coroutine.yield(a)
else
for i=1,n do
--print("iteration=", i, "n=", n, "last=", a[n], "i th=",a[i])
-- put i-th element as the last one
a[n], a[i] = a[i], a[n]
-- generate all permutations of the other elements
permuterate(a, n - 1)
--print("i=", i, "n=", n, "last=", a[n], "i th=",a[i])
-- restore i-th element
a[n], a[i] = a[i], a[n]
end
end
end
function printResult (a)
for i,v in ipairs(a) do
io.write(v, " ")
end
io.write("\n")
end
function createPermuterate(list)
local pick = table.getn(list)
local co = coroutine.create(function () permuterate(list, pick) end)
return function()
local _, value = coroutine.resume(co)
return value
end
end
function createPermuterate1(list)
local pick = table.getn(list)
return coroutine.wrap(function () permuterate(list, pick) end)
end
for p in createPermuterate{"1","2","3";n=3} do
printResult(p)
end
for p in createPermuterate1{1,2,3;n=3} do
printResult(p)
end
algorithm Permutation(array, count)
if array index is 0, display the array
for each item in array until count
swap last and first item
Permutation(array, count -1)
swap last and first item
end
end
now the code
function permuterate (a, n)
if n == 0 then
--printResult(a)
coroutine.yield(a)
else
for i=1,n do
--print("iteration=", i, "n=", n, "last=", a[n], "i th=",a[i])
-- put i-th element as the last one
a[n], a[i] = a[i], a[n]
-- generate all permutations of the other elements
permuterate(a, n - 1)
--print("i=", i, "n=", n, "last=", a[n], "i th=",a[i])
-- restore i-th element
a[n], a[i] = a[i], a[n]
end
end
end
function printResult (a)
for i,v in ipairs(a) do
io.write(v, " ")
end
io.write("\n")
end
function createPermuterate(list)
local pick = table.getn(list)
local co = coroutine.create(function () permuterate(list, pick) end)
return function()
local _, value = coroutine.resume(co)
return value
end
end
function createPermuterate1(list)
local pick = table.getn(list)
return coroutine.wrap(function () permuterate(list, pick) end)
end
for p in createPermuterate{"1","2","3";n=3} do
printResult(p)
end
for p in createPermuterate1{1,2,3;n=3} do
printResult(p)
end
Labels:
coroutine,
Lua,
permutation
Location:
Bangalore, Karnataka, India
Coroutine in Lua - Consumer driven Producer-Consumer implementation
function send(task)
coroutine.yield(task)
end
function receive()
return coroutine.resume(co)
end
function consumer()
while true do
local _, task = receive()
if(task == nil) then break end
print("Processed item", task)
end
end
co = coroutine.create(function ()
local file = io.open("io.txt", "r")
while true do
local task = file:read("*line")
if(task ~= nil) then
send(task)
print("waiting for consumer to signal")
else
break
end
end
end)
consumer()
coroutine.yield(task)
end
function receive()
return coroutine.resume(co)
end
function consumer()
while true do
local _, task = receive()
if(task == nil) then break end
print("Processed item", task)
end
end
co = coroutine.create(function ()
local file = io.open("io.txt", "r")
while true do
local task = file:read("*line")
if(task ~= nil) then
send(task)
print("waiting for consumer to signal")
else
break
end
end
end)
consumer()
Location:
Bangalore, Karnataka, India
Subscribe to:
Comments (Atom)