Giter Site home page Giter Site logo

Comments (3)

lifo101 avatar lifo101 commented on June 26, 2024

Take a look at the example in the docs: https://github.com/lifo101/php-daemon/wiki/Workers#return-value

from php-daemon.

mikuho avatar mikuho commented on June 26, 2024

Hello, I updated my daemon to this...I would like get a PID of the $worker = $this->worker('example'), but I dont know how ($worker->call->getPid())

<?php
require __DIR__ . '/vendor/autoload.php';

use Lifo\Daemon\Daemon;
use Lifo\Daemon\Mediator\Mediator;

use Lifo\Daemon\LogTrait;
use Lifo\Daemon\Promise;

class MyWorker {
  public function getUrl($endPoint) {
    sleep(1); // fake latency

    // To do something...

    // When finished it return url api
    return $endPoint;
  }
}

class MyDaemon extends Daemon {
  public $api = array('www.api1.com', 'www.api2.com', 'www.api3.com', 'www.api4.com', 'www.api5.com', 'www.api6.com');
  public static $runningApi = array();

  public function getApi() {
    static $loop = 1;
    $counter = sizeof($this->api);
    if ($counter >= $loop) {
      $api = $this->api[$loop-1];
    } else {
      $loop = 1;
      $api = $this->api[$loop-1];
    }
    $loop++;
    return $api;
  }

  public function initialize() {
    $this->addWorker(new MyWorker(), 'example')->setAutoRestart(true)->setMaxProcesses(6);
  }

  public function execute() {
      $api = $this->getApi();
      if (!in_array($api, self::$runningApi)) {
        // Here I would like get PID
        $worker = $this->worker('example');
        $pid = // How can get PID of this worker... something like $worker->call->getPid()?
        self::$runningApi[$pid] = $api;

        // the worker method will return a Promise
        $worker->getUrl($api)->then(function($data) {
            //$call = $data->getCall(); and here I would like get PID
            unset(self::$runningApi[$call->getPid()]);
        }, function ($data) {
          if ($data instanceof CallDiedException) {
            $call = $data->getCall();
            unset(self::$runningApi[$call->getPid()]);
          }
        });
      }
  }
}

MyDaemon::getInstance()->setVerbose(true)->setDebug(true)->setLogFile('/tmp/MyDaemon.log')->run();

from php-daemon.

lifo101 avatar lifo101 commented on June 26, 2024

Why? The point of the Daemon is to encapsulate the details of how your workers are forked in the background.

When you create a Worker it can have many forked processes in the background, depending on how you set it up. So, getting a PID from the $worker variable would not be useful since you would only get a PID from one of the many forked processes w/o any relationship to whatever method you're calling.

However, Option 1: In your Worker code, you can add the PID to the returned array|object and act on it in the parent. For example, the getUrl function could return an array like so. But this is only partially useful, since you know the PID of a process that may not even exist anymore, or that PID may be used again for another call to your method (getUrl).

  public function getUrl($endPoint) {
    return [
      'pid' => getpid(),
      'url' => 'https://myurl.com/...'
    ];
  }

Option 2: I'm not sure what you're trying to achieve, but a better way to link the data you're passing to the forked method and your parent is to pass an 'ID' of some sort to the method and then return that same ID in the forked method. (eg: getUrl($id) => return ['url' => 'url', 'id' => $id]).

And btw, please look at the example in the Wiki where I catch the CallDiedException. This is one place you can see the PID of the process.

My advice, forget about PID's.

from php-daemon.

Related Issues (20)

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.