如何使用多个隔离来服务请求
How to use multiple isolates to serve requests
如何将 Dart 服务器设置为使用所有可用核心来处理即将到来的请求(例如,通过使用多个隔离来处理请求)?
使用 HttpServer.bind
的 shared: true
参数。示例:
import 'dart:io';
import 'dart:isolate';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:args/args.dart' show ArgParser;
main(List<String> args) {
var parser = new ArgParser()
..addOption('address', abbr: 'a', defaultsTo: '0.0.0.0')
..addOption('port', abbr: 'p', defaultsTo: '9393')
..addOption('isolates', abbr: 'i', defaultsTo: '3');
var arguments = parser.parse(args);
var nbOfIsolates = int.parse(arguments['isolates']);
for (int i = 1; i < nbOfIsolates; i++) {
Isolate.spawn(_startShelfServer, [arguments['address'], int.parse(arguments['port'])]);
}
_startShelfServer([arguments['address'], int.parse(arguments['port'])]);
}
_startShelfServer(List args) async {
String address = args[0];
int port = args[1];
var helloWorldHandler = (shelf.Request request) => new shelf.Response.ok("Hello World - from isolate ${Isolate.current.hashCode}");
var handler = const shelf.Pipeline()
.addHandler(helloWorldHandler);
var server = await HttpServer.bind(address, port, shared: true);
await shelf_io.serveRequests(server, handler);
print('Serving at http://${server.address.host}:${server.port} - isolate: ${Isolate.current.hashCode}');
}
如何将 Dart 服务器设置为使用所有可用核心来处理即将到来的请求(例如,通过使用多个隔离来处理请求)?
使用 HttpServer.bind
的 shared: true
参数。示例:
import 'dart:io';
import 'dart:isolate';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:args/args.dart' show ArgParser;
main(List<String> args) {
var parser = new ArgParser()
..addOption('address', abbr: 'a', defaultsTo: '0.0.0.0')
..addOption('port', abbr: 'p', defaultsTo: '9393')
..addOption('isolates', abbr: 'i', defaultsTo: '3');
var arguments = parser.parse(args);
var nbOfIsolates = int.parse(arguments['isolates']);
for (int i = 1; i < nbOfIsolates; i++) {
Isolate.spawn(_startShelfServer, [arguments['address'], int.parse(arguments['port'])]);
}
_startShelfServer([arguments['address'], int.parse(arguments['port'])]);
}
_startShelfServer(List args) async {
String address = args[0];
int port = args[1];
var helloWorldHandler = (shelf.Request request) => new shelf.Response.ok("Hello World - from isolate ${Isolate.current.hashCode}");
var handler = const shelf.Pipeline()
.addHandler(helloWorldHandler);
var server = await HttpServer.bind(address, port, shared: true);
await shelf_io.serveRequests(server, handler);
print('Serving at http://${server.address.host}:${server.port} - isolate: ${Isolate.current.hashCode}');
}