【问题标题】:Is it possible to pass command-line arguments to a new isolate from spawnUri()是否可以将命令行参数传递给 spawnUri() 的新隔离
【发布时间】:2012-07-24 06:05:07
【问题描述】:

当使用 spawnUri() 启动新的隔离时,是否可以将命令行参数传递给新的隔离?

例如:命令行:

dart.exe app.dart "Hello World"

在 app.dart 中

#import("dart:isolate");
main() {
  var options = new Options();
  print(options.arguments);    // prints ["Hello World"]
  spawnUri("other.dart");
}

在 other.dart 中

main() {
  var options = new Options();
  print(options.arguments);   // prints [] when spawned from app.dart.
                              // Is it possible to supply 
                              // Options from another isolate?
}

虽然我可以通过它的 SendPort 将数据传递到 other.dart,但我想要的具体用途是使用尚未通过 recievePort 回调创建的另一个 dart 应用程序(例如 pub.dart 或任何其他命令行应用程序)。

【问题讨论】:

  • 另一个重用现有 dart 脚本的选项是一个新进程,传入命令行参数,但具体来说,这个问题是关于产生新的隔离

标签: dart dart-isolates


【解决方案1】:

据我所知,目前的答案是否定的,而且很难通过消息传递来模拟,因为这些选项在 main() 中不可用。

我认为这里有两个很好的功能请求。一个是能够在 spawn() 上传递选项,以便脚本可以从根隔离区或衍生隔离区运行相同的脚本。

另一个可用于实现第一个功能的功能是一种在调用 main() 之前传递由库处理的消息的方法,以便 main() 所依赖的对象可以使用生成的数据进行初始化隔离。

【讨论】:

    【解决方案2】:

    您的示例未使用当前稳定的 SDK 在 other.dart 中调用 print(options.arguments);

    然而

    spanUri("other.dart");
    

    生成一个 Uri。那么spawnUri("other.dart?param=value#orViaHash"); 怎么样,如果你能找到 param/value 对,试试

    print(options.executable);
    print(options.script);
    

    【讨论】:

    • 虽然如果我可以控制 other.dart 这可能会奏效,但我更感兴趣的是我可以在不修改的情况下使用第三方 other.dart。
    猜你喜欢
    • 1970-01-01
    • 2022-11-29
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2021-11-11
    相关资源
    最近更新 更多