turbo.ioloop – Main I/O Loop

Single threaded I/O event loop implementation. The module handles socket events and timeouts and scheduled intervals with millisecond precision

The inner working are as follows:
  • Set iteration timeout to 3600 milliseconds.
  • If there exists any timeout callbacks, check if they are scheduled to be run. Run them if they are. If timeout callback would be delayed because of too long iteration timeout, the timeout is adjusted.
  • If there exists any interval callbacks, check if they are scheduled to be run. If interval callback would be missed because of too long iteration timeout, the iteration timeout is adjusted.
  • If any callbacks exists, run them. If callbacks add new callbacks, adjust the iteration timeout to 0.
  • If there are any events for sockets file descriptors, run their respective handlers. Else wait for specified interval timeout, or any socket events, jump back to start.

Note that because of the fact that the server itself does not know if callbacks block or have a long processing time it cannot guarantee that timeouts and intervals are called on time. In a perfect world they would be called within a reasonable time of what is specified.

Event types for file descriptors are defined in the ioloop module’s namespace:
turbo.ioloop.READ, turbo.ioloop.WRITE, turbo.ioloop.PRI, turbo.ioloop.ERROR
ioloop.instance()
Create or get the global IOLoop instance.
Multiple calls to this function returns the same IOLoop.
Return type:IOLoop class.

IOLoop class

IOLoop is a level triggered I/O loop, with additional support for timeout and time interval callbacks. Heavily influenced by ioloop.py in the Tornado web framework. Note: Only one instance of IOLoop can ever run at the same time!

IOLoop()

Create a new IOLoop class instance.

IOLoop:add_handler(fd, events, handler, arg)

Add handler function for given event mask on fd.

Parameters:
  • fd (Number) – File descriptor to bind handler for.
  • events (Number) – Events bit mask. Defined in ioloop namespace. E.g turbo.ioloop.READ and turbo.ioloop.WRITE. Multiple bits can be AND’ed together.
  • handler (Function) – Handler function.
  • arg – Optional argument for function handler. Handler is called with this as first argument if set.
Return type:

Boolean

IOLoop:update_handler(fd, events)

Update existing handler function’s trigger events.

Parameters:
  • fd (Number) – File descriptor to update.
  • events (Number) – Events bit mask. Defined in ioloop namespace. E.g turbo.ioloop.READ and turbo.ioloop.WRITE. Multiple bits can be AND’ed together.
IOLoop:remove_handler(fd)

Remove a existing handler from the IO Loop.

Parameters:fd (Number) – File descriptor to remove handler from.
IOLoop:add_callback(callback, arg)

Add a callback to be called on next iteration of the IO Loop.

Parameters:
  • callback (Function) – A function to be called on next iteration.
  • arg – Optional argument for callback. Callback is called with this as first argument if set.
Return type:

IOLoop class. Returns self for convinience.

IOLoop:add_timeout(timestamp, func, arg)

Add a timeout with function to be called in future. There is given no gurantees that the function will be called on time. See the note at beginning of this section.

Parameters:
  • timestamp (Number) – Timestamp when to call function, based on Unix CLOCK_MONOTONIC time in milliseconds precision. E.g util.gettimemonotonic() + 3000 will timeout in 3 seconds. See turbo.util.gettimemonotonic().
  • func (Function) – A function to be called after timestamp is reached.
  • arg – Optional argument for func.
Return type:

Unique reference as a reference for this timeout. The reference can be used as parameter for IOLoop:remove_timeout()

IOLoop:remove_timeout(ref)

Remove a scheduled timeout by using its reference.

Parameters:identifer (Number) – Identifier returned by IOLoop:add_timeout()
Return type:Boolean
IOLoop:set_interval(msec, func, arg)

Add a function to be called every milliseconds. There is given no guarantees that the function will be called on time. See the note at beginning of this section.

Parameters:
  • msec (Number) – Milliseconds interval.
  • func (Function) – A function to be called every msecs.
  • arg – Optional argument for func.
Return type:

Unique numeric identifier as a reference to this interval. The refence can be used as parameter for IOLoop:clear_interval()

IOLoop:clear_interval(ref)

Clear a interval.

Parameters:ref (Boolean) – Reference returned by IOLoop:set_interval()
IOLoop:add_signal_handler(signo, handler, arg)

Add a signal handler. If handler already exists for signal it is overwritten. Calling of multiple functions should be handled by user.

Parameters:
  • signo ((Number) Signal number, defined in turbo.signal, e.g turbo.signal.SIGINT.) – The signal number(s) too handle.
  • handler (Function) – Function to handle the signal.
  • arg – Optional argument for handler function.
IOLoop:remove_signal_handler(signo)

Remove a signal handler for specified signal number.

Parameters:signo ((Number) Signal number, defined in turbo.signal, e.g turbo.signal.SIGINT.) – The signal number to remove.
IOLoop:start()

Start the IO Loop. The loop will continue running until IOLoop.close is called via a callback added.

IOLoop:close()

Close the I/O loop. This call must be made from within the running I/O loop via a callback, timeout, or interval. Notice: All pending callbacks and handlers are cleared upon close.

IOLoop:running()

Is the IO Loop running?

Return type:Boolean