GithubHelp home page GithubHelp logo

dart-archive / isolate Goto Github PK

View Code? Open in Web Editor NEW
90.0 34.0 34.0 185 KB

Makes working with Dart isolates easier.

Home Page: https://pub.dev/packages/isolate

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%

isolate's Introduction

Dart CI

DISCONTINUED

This package has been discontinued, and will no longer be maintained.


Helps with isolates and isolate communication in Dart. Requires the dart:isolate library being available. Isolates are not available for Dart on the web.

The package contains individual libraries with different purposes.

Creating send ports and responding to messages.

The "ports.dart" sub-library contains functionality for creating SendPorts and reacting to values sent to those ports.

Working with isolates and running functions in other isolates.

The "isolate_runner.dart" sub-library introduces an IsolateRunner class that gives easy access to the Isolate functionality, and also gives a way to run new functions in the isolate repeatedly, instead of just on the initial spawn call.

A central registry for values that can be used across isolates.

The "registry.dart" sub-library provides a way to create an object registry, and give access to it across different isolates.

Balancing load across several isolates.

The "load_balancer.dart" sub-library can manage multiple Runner objects, including IsolateRunner, and run functions on the currently least loaded runner.

Features and bugs

Please file feature requests and bugs at the issue tracker.

isolate's People

Contributors

2zerosix avatar alexv525 avatar athomas avatar bwilkerson avatar cbracken avatar dependabot[bot] avatar franklinyow avatar kevmoo avatar leafpetersen avatar lrhn avatar mit-mit avatar spkersten avatar srawlins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

isolate's Issues

nnbd version

With 2.12 around the corner I was hoping to see a null safety version of this library.

Remove extra top-level libraries

The modern pattern for most packages is to use a single top-level import library and put the implementation libraries in lib/src. We don't typically ship fine-grained libraries unless there is a specific use case like supporting platform specific imports.

Could you please provide an example of "Registry" use.

Could you please provide an example of "Registry" use.

I would like to access sqlite database on spawed isolate but figured out that this is impossible since spawned isolates cannot use plugins.

So I was thinking if it is possible to share database data using registry in order for spawned isolate uses that to perform a long computation.

Not strong mode clean

Using the latest dartanalyzer, dartanalyzer --strong reveals many strong mode errors (almost all "unsound implicit cast" errors).

fix strong mode warnings

: [error] Type check failed: _createSet (() → Set<dynamic>) is not of type () → Set<int> (package:isolate/registry.dart, line 346, col 34)
[error] Type check failed: _createSet (() → Set<dynamic>) is not of type () → Set<int> (package:isolate/registry.dart, line 373, col 44)
[error] Invalid override. The type of _MultiplexRawReceivePort.handler= (((dynamic) → void) → void) is not a subtype of RawReceivePort.handler= ((Function) → void). (package:isolate/src/raw_receive_port_multiplexer.dart, line 38, col 3)
[warning] Unsound implicit cast from Future<dynamic> to Future<bool> (package:isolate/isolate_runner.dart, line 133, col 12)
[warning] Unsound implicit cast from List<dynamic> to List<Future<dynamic>> (package:isolate/load_balancer.dart, line 101, col 14)
[warning] Unsound implicit cast from List<dynamic> to List<Future<dynamic>> (package:isolate/load_balancer.dart, line 130, col 12)
[warning] Unsound implicit cast from List<dynamic> to List<_LoadBalancerEntry> (package:isolate/load_balancer.dart, line 240, col 14)
[warning] Unsound implicit cast from Future<dynamic> to Future<Capability> (package:isolate/registry.dart, line 128, col 12)
[warning] Unsound implicit cast from Future<dynamic> to Future<bool> (package:isolate/registry.dart, line 150, col 12)
[warning] Unsound implicit cast from List<dynamic> to List<int> (package:isolate/registry.dart, line 164, col 21)
[warning] Unsound implicit cast from Future<dynamic> to Future<List<T>> (package:isolate/registry.dart, line 220, col 12)
[warning] Unsound implicit cast from dynamic to List<int> (package:isolate/registry.dart, line 323, col 18)
[warning] Unsound implicit cast from dynamic to List<int> (package:isolate/registry.dart, line 326, col 21)
[warning] Unsound implicit cast from Future<dynamic> to Future<List<dynamic>> (package:isolate/src/errors.dart, line 89, col 12)
[warning] Unsound implicit cast from Future<dynamic> to Future<List<dynamic>> (package:isolate/src/errors.dart, line 147, col 12)

Unsupported operation: Isolate.resolvePackageUri

When using this with flutter on iOS I get this error

flutter: Unsupported operation: Isolate.resolvePackageUri
flutter: ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
flutter: The following UnsupportedError was thrown:
flutter: Unsupported operation: Isolate.resolvePackageUri
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Isolate.resolvePackageUri (dart:isolate-patch/isolate_patch.dart:343:7)
flutter: #1      Isolate.spawn (dart:isolate-patch/isolate_patch.dart:381:32)
flutter: <asynchronous suspension>
flutter: #2      IsolateRunner.spawn (package:isolate/isolate_runner.dart:58:23)
flutter: <asynchronous suspension>
flutter: #3      initStore (package:tophap_flutter/init.dart:32:59)
flutter: <asynchronous suspension>
flutter: #4      BackgroundWrapper._init.<anonymous closure> (package:tophap_flutter/src/notifications/background.dart:57:42)
flutter: #14     MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart)
flutter: <asynchronous suspension>
flutter: #15     FirebaseNotifications.isForeground (package:firebase_notifications/src/firebase_notifications.dart:87:43)
flutter: #16     BackgroundWrapper._init (package:tophap_flutter/src/notifications/background.dart:54:12)
flutter: <asynchronous suspension>
flutter: #17     onBackground (package:tophap_flutter/src/notifications/background.dart:101:38)
flutter: <asynchronous suspension>
flutter: #18     callbackDispatcher.<anonymous closure>.<anonymous closure> (package:firebase_notifications/src/platform_specifics/ios/background.dart:45:29)
flutter: <asynchronous suspension>
flutter: #23     callbackDispatcher.<anonymous closure> (package:firebase_notifications/src/platform_specifics/ios/background.dart:22:14)
flutter: <asynchronous suspension>
flutter: #24     MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:383:55)
flutter: <asynchronous suspension>
flutter: #25     MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:351:54)
flutter: #26     BinaryMessages.handlePlatformMessage (package:flutter/src/services/platform_messages.dart:76:33)
flutter: <asynchronous suspension>
flutter: #27     _invoke3.<anonymous closure> (dart:ui/hooks.dart:263:15)
flutter: #31     _invoke3 (dart:ui/hooks.dart:262:10)
flutter: #32     _dispatchPlatformMessage (dart:ui/hooks.dart:136:5)
flutter: (elided 16 frames from package dart:async and package dart:async-patch)

i cant pass a function parameter with isolate.spawn and cant pass more than one parameters at time

dart throws an exception that doesn't allow me to pass a function type through isolate.spawn.
ArgumentError (Invalid argument(s): Illegal argument in isolate message : (object is a FunctionType))

client is Socket type from socket.io dart library
isolate = await Isolate.spawn(isolateClient, client);

https://pub.dev/packages/socket_io <-- this library.

so.. i need to listen for a connection and create a thread using isolate to 'isolate the client operations' but it seems like isolate was made just for math stuff in mind.

i also tried passing a List but... i still cant pass this client that seems like a FunctionType

Unhandled Exception: Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function '<anonymous closure>':.)

The exception throw by the code:

  @override
  Future<R> run<R, P>(FutureOr<R>? Function(P argument) function, P argument,
      {Duration? timeout, FutureOr<R> Function()? onTimeout}) {
    return singleResultFuture<R>((SendPort port) {
      _commandPort.send(list4(_run, function, argument, port));
    }, timeout: timeout, onTimeout: onTimeout);
  }

in isolate_runner.dart. What I am curious about is the SendPort can only transfer primitive values.
https://api.dart.dev/stable/2.13.0/dart-isolate/SendPort/send.html

The content of message can be: primitive values (null, num, bool, double, String), instances of SendPort, and lists and maps whose elements are any of these. List and maps are also allowed to contain cyclic references.

So why send the function type argument?

Calling Isolate.spawn thows Unsupported operation

I have a simple dummy function:

void count(int num){ int x = num+1; }

which I'm calling with Isolate.spawn:

Isolate.spawn(count,100);

This causing this exception:

EXCEPTION: Unsupported operation: Isolate.spawn(anonymous function) @ VM63:1
VM63:1 STACKTRACE:(anonymous function) @ VM63:1
VM63:1 #0 Isolate.spawn.<spawn_async_body> (dart:isolate-patch/isolate_patch.dart:326)

1 Future.Future.microtask. (dart:async/future.dart:144)

2 _rootRun (dart:async/zone.dart:891)

3 _ZoneDelegate.run (dart:async/zone.dart:490)

4 NgZoneImpl._run (package:angular2/src/core/zone/ng_zone_impl.dart:154:21)

5 _CustomZone.run (dart:async/zone.dart:790)

6 _CustomZone.runGuarded (dart:async/zone.dart:696)

7 _CustomZone.bindCallback. (dart:async/zone.dart:721)

8 NgZoneImpl._scheduleMicrotask. (package:angular2/src/core/zone/ng_zone_impl.dart:174:11)

9 _rootRun (dart:async/zone.dart:895)

10 _ZoneDelegate.run (dart:async/zone.dart:490)

11 NgZoneImpl._run (package:angular2/src/core/zone/ng_zone_impl.dart:154:21)

12 _CustomZone.run (dart:async/zone.dart:790)

13 _CustomZone.runGuarded (dart:async/zone.dart:696)

14 _CustomZone.bindCallback. (dart:async/zone.dart:721)

15 _microtaskLoop (dart:async/schedule_microtask.dart:41)

16 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)

17 _ScheduleImmediateHelper._handleMutation (dart:html:49290)

"Something" keeps running when isolate TimeOut

Check this simple code:

void main(List<String> arguments) {
  IsolateRunner.spawn().then((isolate) {
    isolate.run(
      _calculate,
      null,
      timeout: Duration(milliseconds: 3000),
      onTimeout: () {
        print('Time out!');
        isolate.close();
        return true;
      },
    ).then((_) {
      isolate.close();
      return "Worked!";
    }).then(print);
  });
}

Future<bool> _calculate(_) async {
  await Future.delayed(Duration(milliseconds: 2000));
  return true;
}

This code works fine and it outputs:

Worked!

Process finished with exit code 0

Cool! But if you change the timeout duration from 3000 to 1000, it outputs:

Time out!
Worked!

...and the program keeps running. It never prints Process finished with exit code 0.
Probably there is something leaking when the timeout methods get called.

IsolateRunner randomly fails to run the given function

@lrhn @bwilkerson @kevmoo

When I run dart sdk.git/sdk/pkg/analyzer/test/file_system/test_all.dart from the command line, it sometimes (like 1 of 20 runs) fails to actually run the function.

With debug output analyzer code it usually prints:

unittest-suite-wait-for-done
before runner.run()
_pathsToTimes is run with [/var/folders/00/0wqkh000h01000cxqpysvccm003ky_/T/test_resourceNllBJF/file1.txt]

But sometimes only

unittest-suite-wait-for-done
before runner.run()
[cut]
PASS: file_system | MemoryResourceProviderTest | test_watch_modifyFile_inSubDir
ERROR: file_system | PhysicalResourceProviderTest | test_getModificationTimes
  Test timed out after 120 seconds.
PASS: file_system | PhysicalResourceProviderTest | test_getStateLocation_uniqueness

The smaller tests suite I run, the less is probability that the function will fail to run, and the test fail.

Patch for debug output:

diff --git a/pkg/analyzer/lib/file_system/physical_file_system.dart b/pkg/analyzer/lib/file_system/physical_file_system.dart
index 8b8618e..15e9243 100644
--- a/pkg/analyzer/lib/file_system/physical_file_system.dart
+++ b/pkg/analyzer/lib/file_system/physical_file_system.dart
@@ -25,6 +25,7 @@ import 'package:watcher/watcher.dart';
  * `-1` is its modification time.
  */
 List<int> _pathsToTimes(List<String> paths) {
+  print('_pathsToTimes is run with $paths');
   return paths.map((path) {
     if (path != null) {
       try {
@@ -83,6 +84,7 @@ class PhysicalResourceProvider implements ResourceProvider {
         .map((source) => source is FileBasedSource ? source.fullName : null)
         .toList();
     IsolateRunner runner = await pathsToTimesIsolateProvider.get();
+    print('before runner.run()');
     return runner.run(_pathsToTimes, paths);
   }

loadBalancer.runMultiple bug

import 'package:isolate/isolate.dart';
import 'package:isolate/load_balancer.dart';

main(List<String> arguments) async {
  var loadBalancer = await LoadBalancer.create(1, ()=>Future.value(Runner()));

  loadBalancer.runMultiple(1,(args){}, null);

  await loadBalancer.close();
}

Unhandled exception:
type List<Object> is not a subtype of type List<Future<dynamic>>

  List<Future> runMultiple(int count, function(argument), argument,
      {Duration timeout, onTimeout(), int load: 100}) {
    ....
    if (count == 1) {
      return list1(run(function, argument,
          load: load, timeout: timeout, onTimeout: onTimeout));
    }
   ...

Is isolate.registry capable for multi cores environment?

I see the comments in the package:

/// When the registry is shared across isolates, both elements and tags must
/// be sendable between the isolates.
/// Between isolates spawned using [Isolate.spawn] from the same initial
/// isolate, most objects can be sent.
/// Only simple objects can be sent between isolates originating from different
/// [Isolate.spawnUri] calls.

Does the initial isolate have to be the root isolate?
If using custom objects instead of base values, will it limit the use for multi cores environment?
If I want to take the advantage of multi-cores, any tips on better to do / not to do when using the registry?

[Enhancement] Unhandled errors stream for LoadBalancer

With single IsolateRunner, it's possible to get a Stream of unhandled errors:

final runner = await IsolateRunner.spawn();
runner.errors.listen((_) {}, onError: print);

It would be nice for LoadBalancer to provide a combined Stream of unhandled errors from all spawned runners, or a public API to access each runner.errors from outside.

tag a release

We need a tagged release for 2.0.0 in order to bring the latest version into the sdk...

Invalid Index on IsolateRunner.kill

If IsolateRunner.kill() is called without setting the timeout duration to 0 seconds, then _commandPort.send(list1(_SHUTDOWN)); is called. However, _handleCommand in IsolateRunnerRemote expects the list to have length of at least 2, so SendPort responsePort = command[1]; is an invalid access which causes a crash.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.