如何处理不包含coroutine.yield()的Lua库?

我想下载一个大文件,并同时处理其他事情。

然而,luasocket.http 从来不调用 coroutine.yield()。当文件下载时,其他所有东西都会冻结。

以下是一个说明性的示例,我尝试同时下载文件和打印一些数字:

local http = require'socket.http'

local downloadRoutine = coroutine.create(function ()
    print 'Downloading large file'
    -- Download an example file
    local url = 'http://ipv4.download.thinkbroadband.com/5MB.zip'
    local result, status = http.request(url)
    print('FINISHED download ('..status..', '..#result..'bytes)')
end)

local printRoutine = coroutine.create(function ()
    -- Print some numbers
    for i=1,10 do
        print(i)
        coroutine.yield()
    end
    print 'FINISHED printing numbers'
end)

repeat
    local printActive = coroutine.resume(printRoutine)
    local downloadActive = coroutine.resume(downloadRoutine)
until not downloadActive and not printActive
print 'Both done!'

运行它会产生这个:

1
Downloading large file
FINISHED download (200, 5242880bytes)
2
3
4
5
6
7
8
9
10
FINISHED printing numbers
Both done!

如您所见, printRoutine 优先执行 resume。它打印数字 1 并 yield。然后重新启动 downloadRoutine,它会下载整个文件而不 yield。然后才打印其余数字。

我不想编写自己的套接字库!我能做什么?

编辑(同一天晚些时候):一些 MUSH 用户也注意到了,他们提供了有用的想法。

点赞
用户312586
用户312586

协程不是线程;协程是合作的,而非同时执行的。当一个协程从另一个协程中 yield 或返回时,它会被阻塞。在普通的 Lua 中,你不能有两个同时执行指针。

然而,你可以使用外部库实现这个效果。其中最流行的是Lua Lanes

2012-11-11 22:07:34
用户1442917
用户1442917

我不明白为什么你不能使用PiL建议或者Copas库(这与这里给出的答案几乎相同)。

Copas封装了socket接口(不是socket.http),但是你可以使用低级接口来获取你需要的东西,类似这样(未测试):

require("socket")
local conn = socket.tcp()
conn:connect("ipv4.download.thinkbroadband.com", 80)
conn:send("GET /5MB.zip HTTP/1.1\n\n")
local file, err = conn:receive()
print(err or file)
conn:close()

然后你可以使用copas的addthread来给你一个非阻塞的socket,使用step/loop函数在有接收到任何数据时执行receive操作。

使用copas较少工作量,而直接使用settimeout(0)则能给你更多控制。

2012-11-12 22:33:41