MultiThreadedEventLoopGroup

public final class MultiThreadedEventLoopGroup : EventLoopGroup

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

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)
  • 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.

    Declaration

    Swift

    public func makeIterator() -> EventLoopIterator

    Return Value

    EventLoopIterator

  • Returns the next EventLoop from this MultiThreadedEventLoopGroup.

    MultiThreadedEventLoopGroup uses round robin across all its EventLoops to select the next one.

    Declaration

    Swift

    public func next() -> EventLoop

    Return Value

    The next EventLoop to use.

  • Shut this MultiThreadedEventLoopGroup down which causes the EventLoops and their associated threads to be shut down and release their resources.

    Even though calling shutdownGracefully more than once should be avoided, it is safe to do so and execution of the handler is guaranteed.

    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.