lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Friday 08 July 2005 5:11 pm, William Trenker wrote:
> Is there a 'standard' way to setup a timer in lua and bind a callback
> function to it?
>
> I'm trying to setup a simple long-running application server using
> copas.  In addition to serving out coroutines to handle the individual
> socket connections, I was hoping to hook into copas to also serve out
> coroutines based on timers for housekeeping, status updates and such.
>
> Thinking about it, I'm really looking for a general coroutine
> dispatcher so I'm probably expecting too much out of copas.  What I'm
> really trying to do is build-in some cron-like support into a socket
> server based on copas and front-ended by Xavante.

i think the easiest way to do it is to create a new task.  copas currently 
creates two tasks, one to check 'ready to read' and other to check 'ready to 
write' sockets.  yours would check 'ready to run' jobs.

something like this (untested!!)

----------- lua code---------------
local runnable_t = {}
function runnable_t:events ()
	return function ()
		if os.time >= myjobs[1].time then
			return table.remove (myjobs, 1)
		else
			return nil
		end
	end
end
function runnable_t:tick (job)
	myrunjob (job)
end
copas.addtask (runnable_t)

--------- end lua code -------------

where myjobs[] is your sorted table of jobs to run, and myjobs[1] is the next 
job

-- 
Javier

Attachment: pgpFXY_N6ximr.pgp
Description: PGP signature