Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix_kill for windows #152

Closed
ttodua opened this issue Jun 8, 2021 · 4 comments
Closed

posix_kill for windows #152

ttodua opened this issue Jun 8, 2021 · 4 comments

Comments

@ttodua
Copy link

ttodua commented Jun 8, 2021

Hi.

  1. library's isSupported() static method checks whether spatie-async is supported on system. Windows misses some requirements, and one of them is posix_kill function. Is is possible to be a support for windows too, using this replacement method for posix_kill ? (though, main problem still stays pcntl_async_signals...)

  2. Do you have an idea, why on ubuntu 20.04 (even though pcntl module is loaded and neither it is in disabled functions) it shows Uncaught Error: Call to undefined function pcntl_async_signals() when calling the function (just for testing purposes)

@samuelgfeller
Copy link

samuelgfeller commented Jul 12, 2021

I work on Windows with XAMPP. Does that mean that I cannot use this library?
If yes, it would be great to add a note about the requirements in the readme.md to save at least someone else's time.

@FelipeAzambuja
Copy link

i develop on my windows machine with wsl

@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

@TheTechsTech
Copy link

@ttodua To get this working on Windows without even messing with posix issue is to make a few changes in Pool.php, ParentRuntime.php, and ParallelProcess.php:

These are the main changes I made from PR to get working on Windows.

// Pool.php

protected static $pcntl = null;

public static function isSupported(): bool
{
  if (\is_null(self::$pcntl))
    self::$pcntl = function_exists('pcntl_async_signals')
      && function_exists('posix_kill') 
      && !self::$forceSynchronous;

  return self::$pcntl;
}

public function wait(?callable $intermediateCallback = null): array
{
  while ($this->inProgress) {
    foreach ($this->inProgress as $process) {
      if ($process->getCurrentExecutionTime() > $this->timeout) {
        $this->markAsTimedOut($process);
      }

      //if ($process instanceof SynchronousProcess) {
      // $this->markAsFinished($process);
      //}

      if (!self::$pcntl) {
        if ($process->isSuccessful()) {
          $this->markAsFinished($process);
        } elseif (! $process->isRunning() && $process->isTerminated()) {
          $this->markAsFailed($process);
        }
      }
    }

    if (! $this->inProgress) {
      break;
    }

    if ($intermediateCallback) {
      call_user_func_array($intermediateCallback, [$this]);
    }

    usleep($this->sleepTime);
  }

  return $this->results;
}

// ParentRuntime.php

public static function createProcess($task, ?int $outputLength = null, ?string $binary = 'php'): Runnable
{
  if (! self::$isInitialised) {
    self::init();
  }

  //if (! Pool::isSupported()) {
  // return SynchronousProcess::create($task, self::getId());
  //}

  $process = new Process([
    $binary,
    self::$childProcessScript,
    self::$autoloader,
    self::encodeTask($task),
    $outputLength,
  ]);

  return ParallelProcess::create($process, self::getId());
}

// ParallelProcess.php

public function stop($timeout = 0): self
{
  $this->process->stop($timeout); // defaults to SIGKILL = 9

  return $this;
}

This will resolve issue #157 too.

TheTechsTech added a commit to symplely/spawn that referenced this issue Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants