Comments (3)
Take a look at the example in the docs: https://github.com/lifo101/php-daemon/wiki/Workers#return-value
from php-daemon.
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.
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)
- Documentation HOT 5
- Worker and pcntl_exec support HOT 9
- Forking Exception: Can't find PDO driver HOT 1
- Arguments to Tasks HOT 4
- How to write unit tests with your daemon? HOT 3
- what happens if a loop is still executing when the interval comes up? HOT 6
- Memory leak in workers HOT 17
- SySV Errors after a while. HOT 2
- Logging Failure after Restart HOT 1
- Premature worker death HOT 1
- Method returns current worker proc count? HOT 1
- Parameters on Task->run causing Fatal Error HOT 2
- Daemon Restart Event Hook HOT 3
- Using callable in worker initialization HOT 2
- Possible memory leak HOT 2
- PHP 8.x HOT 2
- Use with MQTT Server
- How do I pass variables to workers/tasks? HOT 1
- Sysv Error: Error fetching message from queue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-daemon.