Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有更好的方法来;锁;Dart中作为信号灯的端口是否比此示例中的端口更重要?_Dart - Fatal编程技术网

有没有更好的方法来;锁;Dart中作为信号灯的端口是否比此示例中的端口更重要?

有没有更好的方法来;锁;Dart中作为信号灯的端口是否比此示例中的端口更重要?,dart,Dart,在Dart中,除了在端口上启动服务器外,是否可以“锁定”端口。换言之,我猜,端口是作为一个信号灯。或者,是否有其他方法可以达到相同的结果 我发布了一个问题,要求解决这个问题,Fox32建议在一个特定的端口上启动一个服务器,这样可以确定程序的另一个实例是否已经在运行。我需要确定启动实际处理的第一个实例,而不是实际是否正在运行,并且该解决方案可以工作 虽然该解决方案效果良好,但在我看来,应该有一个更适合的解决方案。示例代码如下: /* * Attempt to connect to specifi

在Dart中,除了在端口上启动服务器外,是否可以“锁定”端口。换言之,我猜,端口是作为一个信号灯。或者,是否有其他方法可以达到相同的结果

我发布了一个问题,要求解决这个问题,Fox32建议在一个特定的端口上启动一个服务器,这样可以确定程序的另一个实例是否已经在运行。我需要确定启动实际处理的第一个实例,而不是实际是否正在运行,并且该解决方案可以工作

虽然该解决方案效果良好,但在我看来,应该有一个更适合的解决方案。示例代码如下:

/*
 * Attempt to connect to specific port to determine if first process.
 */
async.Future<bool> fTestIfFirstInstance() {

  async.Completer<bool> oCompleter = new async.Completer<bool>();

  const String S_HOST = "127.0.0.1"; // ie: localhost 
  const int    I_PORT = 8087;

  HttpServer.bind(S_HOST, I_PORT).then((oHtServer) {
    ogHtServer = oHtServer;        // copy to global
    oCompleter.complete(true);     // this is the first process
    return;
  }).catchError((oError) {
    oCompleter.complete(false);    // this is NOT the first process
    return;
  });
  return oCompleter.future;
}
/*
*尝试连接到特定端口以确定是否为第一个进程。
*/
async.Future fTestIfFirstInstance(){
async.Completer-oCompleter=new async.Completer();
常量字符串S_HOST=“127.0.0.1”;//ie:localhost
const int I_端口=8087;
绑定(S_主机,I_端口)。然后((oHtServer){
ogHtServer=oHtServer;//复制到全局
oCompleter.complete(true);//这是第一个进程
返回;
}).catchError((oError){
oCompleter.complete(false);//这不是第一个进程
返回;
});
返回oCompleter.future;
}

这通常通过使用文件来完成,例如,/tmp/my_program.lock”或“~/.my_program.lock”,具体取决于全局锁或每用户锁

我会简单地说:

bool isRunning() {
  return new File(lockPath).existsSync();
}
开始:

void createLock() {
  if (isRunning()) throw "Lock file '$lockPath' exists, program may be running";
  new File(lockPath).createSync();
}
关闭程序时:

void deleteLock() {
  new File(lockPath).deleteSync();
}
需要记住的是,虽然HttpServer将在程序关闭时关闭,但文件不会被删除。这可以通过在创建文件时将程序PID写入锁定文件来解决,并检查PID是否在
isRunning
中处于活动状态。如果该文件不存在,请删除该文件并返回false。

我不确定该(RawServerSocket)是否比HttpServer解决方案“更好”,但它可能更有意义

我认为简单地“锁定”端口并解锁端口是值得的。它提供了一种使用信号量IMHO的好方法

/*
 * Attempt to connect to specific port to determine if first process.
 */
async.Future<bool> fTestIfFirstInstance() {
  async.Completer<bool> oCompleter = new async.Completer<bool>(); 
  RawServerSocket.bind("127.0.0.1", 8087).then((oSocket) {
    ogSocket = oSocket;         // assign to global
    oCompleter.complete(true);
  }).catchError((oError) {
    oCompleter.complete(false);
  });

  return oCompleter.future;
}
/*
*尝试连接到特定端口以确定是否为第一个进程。
*/
async.Future fTestIfFirstInstance(){
async.Completer-oCompleter=new async.Completer();
绑定(“127.0.0.1”,8087)。然后((oSocket){
ogSocket=oSocket;//分配给全局
oCompleter.complete(正确);
}).catchError((oError){
oCompleter.complete(假);
});
返回oCompleter.future;
}

谢谢安德斯。很高兴知道实现这一目标的另一种方法。但是我不会说这一定比IMHO好。也许Dart将来可以提供一些东西来更优雅地实现这一点。你可以使用tcp套接字而不是http服务器。好的,我来看看。Http似乎有些过分,但我以前没有使用过tcp套接字。经过反思,我的代码也相当冗长。我把它砍掉了。我认为虚拟机必须先经历这一过程,以确定未来,然后异步运行代码,因此对于初学者来说,不需要两个“返回”,因为最终返回将在异步代码“再次”之后执行。我计划找到一个比Http更好的解决方案,如果可行的话,我可能会在较低的级别编写一些东西来锁定端口。不过,它确实做到了这一点,而且可以无缝地工作。