Skip to content

Instantly share code, notes, and snippets.

@Mons
Created February 12, 2021 11:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mons/a7bde7d475c5fc69003352ae55ba27b7 to your computer and use it in GitHub Desktop.
Save Mons/a7bde7d475c5fc69003352ae55ba27b7 to your computer and use it in GitHub Desktop.
local client = require 'http.client'
local fiber = require 'fiber'
local yaml = require 'yaml'
local uris = {
'http://httpbin.org/get?1',
'http://httpbin.org/get?2',
'http://httpbin.org/get?3',
'http://httpbin.org/get?4',
'http://httpbin.org/get?5',
}
local max_parallel = 3
local results = fiber.channel()
local active = 0
local fibers = {}
local n = 0
for i=1, max_parallel do
fiber.create(function()
while true do
n = n + 1
local uri = uris[n]
print("fetch ", uri)
if not uri then break end
active = active + 1
print("Start request in", i, n, "active", active)
local res = client.get(uri)
print("End request in", i, res.status, "active", active)
results:put(res)
active = active - 1
if active == 0 then
results:close()
end
end
end)
end
local res = {}
repeat
local result = results:get()
print("Got from results", result and result.status)
table.insert(res, result)
until not result
-- here we have all results in res
os.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment