MultiThreadedEventLoopGroup

final public class MultiThreadedEventLoopGroup : EventLoopGroup

An EventLoopGroup which will create multiple EventLoops, each tied to its own Thread.

The effect of initializing a MultiThreadedEventLoopGroup is to spawn numberOfThreads fresh threads which will all run their own EventLoop. Those threads will not be shut down until shutdownGracefully or syncShutdownGracefully is called.

Note

It’s good style to call MultiThreadedEventLoopGroup.shutdownGracefully or MultiThreadedEventLoopGroup.syncShutdownGracefully when you no longer need this EventLoopGroup. In many cases that is just before your program exits.

Warning

Unit tests often spawn one MultiThreadedEventLoopGroup per unit test to force isolation between the tests. In those cases it’s important to shut the MultiThreadedEventLoopGroup down at the end of the test. A good place to start a MultiThreadedEventLoopGroup is the setUp method of your XCTestCase subclass, a good place to shut it down is the tearDown method.
  • Creates a MultiThreadedEventLoopGroup instance which uses numberOfThreads.

    Note

    Don’t forget to call shutdownGracefully or syncShutdownGracefully when you no longer need this EventLoopGroup. If you forget to shut the EventLoopGroup down you will leak numberOfThreads (kernel) threads which are costly resources. This is especially important in unit tests where one MultiThreadedEventLoopGroup is started per test case.

  • arguments:

    • numberOfThreads: The number of Threads to use.
  • Declaration

    Swift

    public convenience init(numberOfThreads: Int)
  • Creates a MultiThreadedEventLoopGroup instance which uses numThreads.

    • arguments:
      • numThreads: The number of Threads to use.

    Declaration

    Swift

    @available(*, deprecated, renamed: "init(numberOfThreads:﹚")
    public convenience init(numThreads: Int)
  • Returns the EventLoop for the calling thread.

    Declaration

    Swift

    public static var currentEventLoop: EventLoop? { get }

    Return Value

    The current EventLoop for the calling thread or nil if none is assigned to the thread.

  • Returns an EventLoopIterator over the EventLoops in this MultiThreadedEventLoopGroup.

    Note

    The return value of makeIterator is currently optional as requiring it would be SemVer major. From NIO 2.0.0 on it will return a non-optional iterator.

    Declaration

    Swift

    public func makeIterator() -> EventLoopIterator?

    Return Value

    EventLoopIterator

  • Declaration

    Swift

    public func next() -> EventLoop
  • Shut this MultiThreadedEventLoopGroup down which causes the EventLoops and their associated threads to be shut down and release their resources.

    Declaration

    Swift

    public func shutdownGracefully(queue: DispatchQueue, _ handler: @escaping (Error?) -> Void)

    Parameters

    queue

    The DispatchQueue to run handler on when the shutdown operation completes.

    handler

    The handler which is called after the shutdown operation completes. The parameter will be nil on success and contain the Error otherwise.