Update: mod
This commit is contained in:
parent
fd6c593066
commit
9aaba0eca6
34
README.md
34
README.md
@ -10,38 +10,42 @@ Usage Async
|
|||||||
```lua
|
```lua
|
||||||
async = async.Async()
|
async = async.Async()
|
||||||
```
|
```
|
||||||
2. set the priority of the async pool to high.
|
2. max time for thread before yielding (maxtime is in milliseconds).
|
||||||
```lua
|
```lua
|
||||||
async.priority(50, 500)
|
async.maxtime = 200
|
||||||
```
|
```
|
||||||
3. iterate from 1 to 50 and log the value i.
|
3. queue_threads is the amount of active threads when you run function queue_task.
|
||||||
|
```lua
|
||||||
|
async.queue_threads = 8
|
||||||
|
```
|
||||||
|
4. iterate from 1 to 50 and log the value i.
|
||||||
```lua
|
```lua
|
||||||
async.iterate(1, 50, function(i)
|
async.iterate(1, 50, function(i)
|
||||||
minetest.log(i)
|
minetest.log(i)
|
||||||
end)
|
end, function() minetest.log("Callback") end)
|
||||||
```
|
```
|
||||||
4. run throught each element in a table.
|
5. run throught each element in a table.
|
||||||
```lua
|
```lua
|
||||||
local array = {"start", "text2", "text3", "text4", "text5", "end"}
|
local array = {"start", "text2", "text3", "text4", "text5", "end"}
|
||||||
async.foreach(array, function(k, v)
|
async.foreach(array, function(k, v)
|
||||||
minetest.log(v)
|
minetest.log(v)
|
||||||
end)
|
end, function() minetest.log("Callback") end)
|
||||||
```
|
```
|
||||||
5. async do while loop.
|
6. async do while loop.
|
||||||
```lua
|
```lua
|
||||||
local c = 50
|
local c = 50
|
||||||
async.do_while(function() return c > 0 end, function()
|
async.do_while(function() return c > 0 end, function()
|
||||||
minetest.log(c)
|
minetest.log(c)
|
||||||
c = c - 1
|
c = c - 1
|
||||||
end)
|
end, function() minetest.log("Callback") end)
|
||||||
```
|
```
|
||||||
6. register a async globalstep. this one spams the chat with the word spam.
|
7. register a async globalstep. this one spams the chat with the word spam.
|
||||||
```lua
|
```lua
|
||||||
async.register_globalstep(function(dtime)
|
async.register_globalstep(function(dtime)
|
||||||
minetest.chat_send_all("spam")
|
minetest.chat_send_all("spam")
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
7. chain task runs a group of functions from a table.
|
8. chain task runs a group of functions from a table.
|
||||||
```lua
|
```lua
|
||||||
async.chain_task({
|
async.chain_task({
|
||||||
function(args)
|
function(args)
|
||||||
@ -53,17 +57,17 @@ async.chain_task({
|
|||||||
args.count = args.count + 1
|
args.count = args.count + 1
|
||||||
minetest.log(args.count)
|
minetest.log(args.count)
|
||||||
return args
|
return args
|
||||||
end})
|
end}, function(args) minetest.log(args.count) end)
|
||||||
```
|
```
|
||||||
8. adds a single function to the task queue. This is a sort of waiting list.
|
9. adds a single function to the task queue. This is a sort of waiting list.
|
||||||
```lua
|
```lua
|
||||||
async.queue_task(function()
|
async.queue_task(function()
|
||||||
minetest.log("Hello World!")
|
minetest.log("Hello World!")
|
||||||
end)
|
end, function(args) minetest.log("callback") end)
|
||||||
```
|
```
|
||||||
9. Same as queue_task but the task does not go into a queue.
|
10. Same as queue_task but the task does not go into a queue.
|
||||||
```lua
|
```lua
|
||||||
async.single_task(function()
|
async.single_task(function()
|
||||||
minetest.log("Hello World!")
|
minetest.log("Hello World!")
|
||||||
end)
|
end, function(args) minetest.log("callback") end)
|
||||||
```
|
```
|
||||||
|
73
async.lua
73
async.lua
@ -1,54 +1,53 @@
|
|||||||
|
local threads = {}
|
||||||
|
local time = 0
|
||||||
|
minetest.register_globalstep(function(dtime)
|
||||||
|
time = time + dtime
|
||||||
|
if #threads < 1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
for i = #threads, 1, -1 do
|
||||||
|
if time >= threads[i].delay then
|
||||||
|
local thread = threads[i].thread
|
||||||
|
local state = coroutine.status(thread)
|
||||||
|
if state == "dead" then
|
||||||
|
table.remove(threads, i)
|
||||||
|
elseif state == "suspended" then
|
||||||
|
coroutine.resume(thread)
|
||||||
|
threads[i].delay = time + threads[i].delaymax
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
function async.Async()
|
function async.Async()
|
||||||
local self = {}
|
local self = {}
|
||||||
|
|
||||||
self.task_queue = {}
|
self.task_queue = {}
|
||||||
self.resting = 200
|
|
||||||
self.maxtime = 200
|
self.maxtime = 200
|
||||||
self.queue_threads = 8
|
self.queue_threads = 8
|
||||||
self.state = "suspended"
|
|
||||||
|
|
||||||
self.create_worker = function(func)
|
self.create_worker = function(func)
|
||||||
local thread = coroutine.create(func)
|
local thread = coroutine.create(func)
|
||||||
if not thread or coroutine.status(thread) == "dead" then
|
if not thread or coroutine.status(thread) == "dead" then
|
||||||
minetest.after(0.3, self.create_worker, func)
|
minetest.after(0.5, self.create_worker, func)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.run_worker(thread)
|
threads[#threads + 1] = {
|
||||||
|
thread = thread,
|
||||||
|
delay = time + self.maxtime / 1000,
|
||||||
|
delaymax = self.maxtime / 1000
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
self.create_globalstep_worker = function(func)
|
self.create_globalstep_worker = function(func)
|
||||||
local thread = coroutine.create(func)
|
local thread = coroutine.create(func)
|
||||||
if not thread or coroutine.status(thread) == "dead" then
|
if not thread or coroutine.status(thread) == "dead" then
|
||||||
minetest.after(0.3, self.create_globalstep_worker, func)
|
minetest.after(0.5, self.create_globalstep_worker, func)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.run_globalstep_worker(thread)
|
threads[#threads + 1] = {
|
||||||
|
thread = thread,
|
||||||
|
delay = time,
|
||||||
|
delaymax = 0
|
||||||
|
}
|
||||||
end
|
end
|
||||||
self.run_worker = function(thread)
|
|
||||||
if not thread or coroutine.status(thread) == "dead" then
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
coroutine.resume(thread)
|
|
||||||
minetest.after(self.resting / 1000, self.run_worker, thread)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.run_globalstep_worker = function(thread)
|
|
||||||
if not thread or coroutine.status(thread) == "dead" then
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
coroutine.resume(thread)
|
|
||||||
minetest.after(0, self.run_globalstep_worker, thread)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.priority = function(resting, maxtime)
|
|
||||||
self.resting = resting
|
|
||||||
self.maxtime = maxtime
|
|
||||||
end
|
|
||||||
|
|
||||||
self.iterate = function(from, to, func, callback)
|
self.iterate = function(from, to, func, callback)
|
||||||
self.create_worker(function()
|
self.create_worker(function()
|
||||||
local last_time = minetest.get_us_time() / 1000
|
local last_time = minetest.get_us_time() / 1000
|
||||||
@ -69,7 +68,6 @@ function async.Async()
|
|||||||
return
|
return
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.foreach = function(array, func, callback)
|
self.foreach = function(array, func, callback)
|
||||||
self.create_worker(function()
|
self.create_worker(function()
|
||||||
local last_time = minetest.get_us_time() / 1000
|
local last_time = minetest.get_us_time() / 1000
|
||||||
@ -90,7 +88,6 @@ function async.Async()
|
|||||||
return
|
return
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.do_while = function(condition_func, func, callback)
|
self.do_while = function(condition_func, func, callback)
|
||||||
self.create_worker(function()
|
self.create_worker(function()
|
||||||
local last_time = minetest.get_us_time() / 1000
|
local last_time = minetest.get_us_time() / 1000
|
||||||
@ -111,7 +108,6 @@ function async.Async()
|
|||||||
return
|
return
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.register_globalstep = function(func)
|
self.register_globalstep = function(func)
|
||||||
self.create_globalstep_worker(function()
|
self.create_globalstep_worker(function()
|
||||||
local last_time = minetest.get_us_time() / 1000000
|
local last_time = minetest.get_us_time() / 1000000
|
||||||
@ -127,7 +123,6 @@ function async.Async()
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.chain_task = function(tasks, callback)
|
self.chain_task = function(tasks, callback)
|
||||||
self.create_worker(function()
|
self.create_worker(function()
|
||||||
local pass_arg = nil
|
local pass_arg = nil
|
||||||
@ -149,7 +144,6 @@ function async.Async()
|
|||||||
return
|
return
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.queue_task = function(func, callback)
|
self.queue_task = function(func, callback)
|
||||||
table.insert(self.task_queue, {func = func, callback = callback})
|
table.insert(self.task_queue, {func = func, callback = callback})
|
||||||
if self.queue_threads > 0 then
|
if self.queue_threads > 0 then
|
||||||
@ -182,7 +176,6 @@ function async.Async()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self.single_task = function(func, callback)
|
self.single_task = function(func, callback)
|
||||||
self.create_worker(function()
|
self.create_worker(function()
|
||||||
local pass_arg = func()
|
local pass_arg = func()
|
||||||
|
Loading…
Reference in New Issue
Block a user