diff --git a/docs/changes.md b/docs/changes.md index e835fcd..b09d3d0 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -84,6 +84,7 @@ Allows the user to have multi auto set priorities (Requires chronos). Also adds Added --- +- multi:setTaskDelay(delay), Tasks which are now tied to a processor can have an optional delay between the execution between each task. Useful perhaps for rate limiting. Without a delay all grouped tasks will be handled in one step. - processor's now have a boost function which causes it to run its processes the number of times specified in the `boost(count)` function - thread.hold will now use a custom hold method for objects with a `Hold` method. This is called like `obj:Hold(opt)`. The only argument passed is the optional options table that thread.hold can pass. There is an exception for connection objects. While they do contain a Hold method, the Hold method isn't used and is there for proxy objects, though they can be used in non proxy/thread situations. Hold returns all the arguments that the connection object was fired with. - shared_table = STP:newSharedTable(tbl_name) -- Allows you to create a shared table that all system threads in a process have access to. Returns a reference to that table for use on the main thread. Sets `_G[tbl_name]` on the system threads so you can access it there. diff --git a/init.lua b/init.lua index c0f75a6..9e70cfd 100644 --- a/init.lua +++ b/init.lua @@ -1085,6 +1085,7 @@ function multi:newProcessor(name, nothread, priority) c.Mainloop = {} c.Type = multi.registerType("process", "processes") local Active = nothread or false + local task_delay = 0 c.Name = name or "" c.tasks = {} c.threads = {} @@ -1176,6 +1177,10 @@ function multi:newProcessor(name, nothread, priority) c.process:Destroy() end + function c:setTaskDelay(delay) + task_delay = tonumber(delay) or 0 + end + c:newThread("Task Handler", function() local self = multi:getCurrentProcess() local function task_holder() @@ -1187,6 +1192,9 @@ function multi:newProcessor(name, nothread, priority) else thread.hold(task_holder) end + if task_delay~=0 then + thread.sleep(task_delay) + end end end).OnError(multi.error) @@ -2520,6 +2528,10 @@ end threadManager = multi:newProcessor("Global_Thread_Manager", nil, true).Start() threadManager.tasks = multi.tasks -- The main multi interface is a bit different. +function multi:setTaskDelay(delay) + threadManager:setTaskDelay(delay) +end + function multi:getThreadManagerProcess() return threadManager end diff --git a/tests/test.lua b/tests/test.lua index c1b0192..62c793f 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -98,11 +98,18 @@ multi, thread = require("multi"):init{print=true,warn=true,error=true,debugging= -- multi:newTLoop(func, 1) -- multi:mainloop() -for i = 1, 100 do + +multi:setTaskDelay(.05) +multi:newTask(function() + for i = 1, 100 do + multi:newTask(function() + print("Task "..i) + end) + end multi:newTask(function() - print("Task "..i) + multi:Stop() end) -end +end) local conn = multi:newConnection() conn(function() print("Test 1") end)