Skip to content

Instantly share code, notes, and snippets.

@meshula
Created March 15, 2023 02:06
Show Gist options
  • Save meshula/1b12ef3c17d5ce9d52b3d60734b72381 to your computer and use it in GitHub Desktop.
Save meshula/1b12ef3c17d5ce9d52b3d60734b72381 to your computer and use it in GitHub Desktop.
test-nanoexrinfo.lua
local ThreadPool = {}
ThreadPool.__index = ThreadPool
function ThreadPool.new(num_workers)
local self = setmetatable({}, ThreadPool)
self.num_workers = num_workers
self.workers = {}
self.tasks = {}
self.running = true
for i = 1, num_workers do
table.insert(self.workers, coroutine.create(function()
while self.running do
local task = table.remove(self.tasks, 1)
if task then
task()
else
coroutine.yield()
end
end
end))
end
return self
end
function ThreadPool:add_task(task)
table.insert(self.tasks, task)
for i = 1, self.num_workers do
coroutine.resume(self.workers[i])
end
end
function ThreadPool:wait()
while #self.tasks > 0 do
for i = 1, self.num_workers do
coroutine.resume(self.workers[i])
end
end
end
local lfs = require "lfs"
function recurse_directory(directory_path, thread_pool)
for entry in lfs.dir(directory_path) do
if entry ~= "." and entry ~= ".." then
local path = directory_path .. "/" .. entry
local mode = lfs.attributes(path, "mode")
if mode == "directory" then
-- Recursively process the subdirectory in a new task
thread_pool:add_task(function()
recurse_directory(path, thread_pool)
end)
elseif mode == "file" and string.sub(entry, -4) == ".exr" then
print("Submitting " .. path)
-- Print the file name in a new task
thread_pool:add_task(function()
print_file_name(path)
end)
end
end
end
end
function main()
local directory_path = arg[1]
if not directory_path then
print("Usage: lua main.lua <directory_path>")
return
end
local thread_pool = ThreadPool.new(4)
recurse_directory(directory_path, thread_pool)
thread_pool:wait()
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment