【问题标题】:PHP, wake up process which is sleepingPHP,唤醒正在休眠的进程
【发布时间】:2014-08-02 16:21:54
【问题描述】:

我有两个同时运行的 PHP 脚本。其中之一 (SleepingScript.php) 调用 sleep 函数。如有必要,另一个脚本 (WakeScript.php) 应该能够唤醒正在休眠的脚本。

我怎样才能做到这一点?

这里有一些代码可以更好地解释这种情况。

SleepingScript.php

<?php
   $myPid = getmypid();
   /* save $myPid to database or file */
   sleep( 120 );
   /* other statements executed after 2 minutes or when woken up by WakeScript */
?>

WakeScript.php

<?php
    /* ... */
    if( $wakeUpNeeded ) {
      $pid = readSavedPid();
      wake( $pid );
    }
?>

【问题讨论】:

  • 不是重复但密切相关的how-can-i-stop-php-sleep...
  • 没有简单的方法可以直接唤醒“休眠”脚本。然而,想象一下“睡眠”脚本“每秒醒来”并检查是否存在某些东西,比如文件?例如创建一个文件。每秒检查文件是否存在于睡眠脚本中。当您希望脚本“唤醒”时删除文件。还有很多其他便宜的方法。这是有用的方法。

标签: php sleep wakeup


【解决方案1】:

这可以使用进程信号 SIGCONT 来通知脚本它需要唤醒来完成。请注意,这需要 php 扩展 pcntl 并且可能取决于平台(在 macOS Big Sur 上测试):

SleepingScript.php

<?php

if (!extension_loaded('pcntl')) {
    echo 'Unable to wake up!\n';
} else {
    // While we aren't doing anything with the CONT signal, we need
    // it to be handled so that the process can wake up from sleep
    pcntl_signal(SIGCONT, function () {});
}

// Save this to file, redis, or something else
echo posix_getpid() . "\n";

// Sometimes time_nanosleep isn't available on your platform
// If it's not, you'll need to substitute using sleep/usleep and maybe hrtime
$seconds = 120;
$nanoseconds = 0;
$nano = time_nanosleep($seconds, $nanoseconds);

if ($nano === true) {
    echo "Slept for $seconds seconds, $nanoseconds nanoseconds.\n";
} elseif ($nano === false) {
    echo "Sleeping failed.\n";
} else {
    echo "Interrupted by a signal.\n";
    echo "Time remaining: {$nano['seconds']} seconds, {$nano['nanoseconds']} nanoseconds.";
}

WakeScript.php

<?php

// Get saved pid. This could be from file, redis, or something else.
// Here we're going to use the first argument for ease
$pid = (int)$argv[1];

// While normally used to kill a process, posix_kill really just sends a signal.
// We'll send SIGCONT to wake up the other script
posix_kill($pid, SIGCONT);

这就是它在行动中的样子。第一合一终端:

$ php SleepingScript.php 
93357

然后去第二个终端:

$ php WakeScript.php 93357

然后回到原来的终端:

$ php SleepingScript.php 
93357
Interrupted by a signal.
Time remaining: 111 seconds, 712678616 nanoseconds.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 2022-01-03
    • 2015-06-28
    • 2011-08-05
    • 2012-08-15
    • 1970-01-01
    • 2014-07-15
    • 2015-05-28
    相关资源
    最近更新 更多