lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have another problem with threading.

this code comes from http://www.lua.org/pil/9.4.html
and it fails in select().
the interpreter binary is from Debian sid which has no thread func.

                    --- Okajima, Jun. Tokyo, Japan.

------------------------------------------
okajima@Debian:~$ lua 94.lua
lua: attempt to index a string value
stack traceback:
        [C]: in function `select'
        94.lua:57: in function `dispatcher'
        94.lua:70: in main chunk
        [C]: ?
okajima@Debian:~$ lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> require("socket")
> socket.select(nil, nil, 0.1)
> socket.select(nil, nil, 1)
> socket.select(nil, nil, 2)
okajima@Debian:~$ cat -n 94.lua
     1  require "socket"
     2
     3  -- warning: depending on the version of luasocket that we use,
     4  -- we have to change `settimeout' to `timeout' (as done here)
     5
     6
     7  function receive (connection)
     8    connection:timeout(0)   -- do not block
     9    local s, status = connection:receive(2^10)
    10    if status == "timeout" then
    11      coroutine.yield(connection)
    12    end
    13    return s, status
    14  end
    15
    16
    17  function download (host, file)
    18    local c = assert(socket.connect(host, 80))
    19    local count = 0    -- counts number of bytes read
    20    c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
    21    while true do
    22      local s, status = receive(c)
    23      count = count + string.len(s)
    24      if status == "closed" then break end
    25    end
    26    c:close()
    27    print(file, count)
    28  end
    29
    30
    31  threads = {}    -- list of all live threads
    32  function get (host, file)
    33    -- create coroutine
    34    local co = coroutine.create(function ()
    35      download(host, file)
    36    end)
    37    -- insert it in the list
    38    table.insert(threads, co)
    39  end
    40
    41
    42  function dispatcher ()
    43    while true do
    44      local n = table.getn(threads)
    45      if n == 0 then break end   -- no more threads to run
    46      local connections = {}
    47      for i=1,n do
    48        local status, res = coroutine.resume(threads[i])
    49        if not res then    -- thread finished its task?
    50          table.remove(threads, i)
    51          break
    52        else    -- timeout
    53          table.insert(connections, res)
    54        end
    55      end
    56      if table.getn(connections) == n then
    57        socket.select(connections)
    58      end
    59    end
    60  end
    61
    62
    63  host = "www.w3.org"
    64
    65  get(host, "/TR/html401/html40.txt")
    66  get(host,"/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
    67  get(host,"/TR/REC-html32.html")
    68  get(host, "/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")
    69
    70  dispatcher()   -- main loop
    71
    72
----------------------------