Sunday 15 March 2015

sockets - Copas and iterators using coroutines -



sockets - Copas and iterators using coroutines -

i trying adapt nrk/redis-lua utilize copas. @ first, seemed easy me (at to the lowest degree commands get, set): wrap client using copas.wrap().

it not work, because calls receive used within iterators using coroutines. illustration below shows happens:

local copas = require "copas" local socket = require "socket" local function iterate (x) local client = socket.tcp () client:connect("127.0.0.1", 6379) client:settimeout (0) client = copas.wrap (client) homecoming coroutine.wrap (function () k, v in pairs (x) client:receive "*l" coroutine.yield (k, v) end client:close () end) end local info = {} = 1, 2 info [i] = end local function worker () local id = coroutine.running () k, v in iterate (data) print (id, k, v) end end copas.addthread (worker) copas.addthread (worker) copas.loop ()

the output is:

thread: 0x1b31990 tcp{client}: 0x1b31e48 table: 0x1b2eac0 thread: 0x1b31990 tcp{client}: 0x1b31e48 table: 0x1b2eac0 ...

first question: had set client:settimeout (0), otherwise phone call client:receive blocking. creates active wait... there improve way?

second question: there way prepare problem of yielding within iterator? can rewrite iterator closure avoid using coroutine, there less intrusive way of solving problem?

sockets lua iterator coroutine

No comments:

Post a Comment