MultiThreadedEventLoopGroup
public final class MultiThreadedEventLoopGroup : EventLoopGroup
extension MultiThreadedEventLoopGroup: CustomStringConvertible
An EventLoopGroup
which will create multiple EventLoop
s, 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 callMultiThreadedEventLoopGroup.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 oneMultiThreadedEventLoopGroup
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 usesnumberOfThreads
.Note
Don’t forget to call
shutdownGracefully
orsyncShutdownGracefully
when you no longer need thisEventLoopGroup
. If you forget to shut theEventLoopGroup
down you will leaknumberOfThreads
(kernel) threads which are costly resources. This is especially important in unit tests where oneMultiThreadedEventLoopGroup
is started per test case.arguments:
- numberOfThreads: The number of
Threads
to use.
- numberOfThreads: The number of
Declaration
Swift
public convenience init(numberOfThreads: Int)
-
Returns an
EventLoopIterator
over theEventLoop
s in thisMultiThreadedEventLoopGroup
.Declaration
Swift
public func makeIterator() -> EventLoopIterator
Return Value
-
Shut this
MultiThreadedEventLoopGroup
down which causes theEventLoop
s 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 thehandler
is guaranteed.Declaration
Swift
public func shutdownGracefully(queue: DispatchQueue, _ handler: @escaping (Error?) -> Void)
Parameters
queue
The
DispatchQueue
to runhandler
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 theError
otherwise. -
Convert the calling thread into an
EventLoop
.This function will not return until the
EventLoop
has stopped. You can initiate stopping theEventLoop
by callingeventLoop.shutdownGracefully
which will eventually make this function return.Declaration
Swift
public static func withCurrentThreadAsEventLoop(_ callback: @escaping (EventLoop) -> Void)
Parameters
callback
-
Declaration
Swift
public var description: String { get }