WyriHaximus / reactphp-child-process-pool

MIT License
40 stars 9 forks source link

MessageFactory doesn't seem to resolve #15

Open factormaarten opened 6 years ago

factormaarten commented 6 years ago

Hi Cees-Jan,

I'm trying to get the pool running, but something isn't adding up for me. This is the class I'm calling:

<?php

namespace Database;

use React\EventLoop\LoopInterface;
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
use function React\Promise\resolve;

class PhalconSQL implements ChildInterface
{
    private $phql;

    public static function create(Messenger $messenger, LoopInterface $loop)
    {
        $messenger->registerRpc('executeQuery', function (Payload $payload) {
            return resolve([
                'response' => 'works'
            ]);
        });
    }
}

Here's how I'm calling the rpc:

                    $loop = EventLoopFactory::create();
                    CpuCoreCountFlexible::createFromClass(PhalconSQL::class, $loop)->then(function (PoolInterface $pool) {
                        echo 'tack' . PHP_EOL;

                        $pool->rpc(
                            MessageFactory::rpc('executeQuery')
                        )->then(function (Payload $result){
                            echo $result['response'] . PHP_EOL;
                            $pool->terminate();
                        });

                        echo 'tock' . PHP_EOL;

                    });
                    $loop->run();

What should I change to make it all resolve?

Thanks so much! Maarten

Attilathekilla commented 6 years ago

I too would be interested in the solution here

WyriHaximus commented 6 years ago

Could you replace the then's with done, and post the errors here?

factormaarten commented 6 years ago

I've tried that, but it's not giving any errors unfortunately

WyriHaximus commented 6 years ago

That is weird, there should always be something. I'll run your code tonight and see if it works on my end

WyriHaximus commented 6 years ago

With some minor edits it works:


$loop = EventLoopFactory::create();
CpuCoreCountFlexible::createFromClass(PhalconSQL::class, $loop)->done(function (PoolInterface $pool) {
    echo 'tack' . PHP_EOL;

    $pool->rpc(
        MessagesFactory::rpc('executeQuery')
    )->done(function (Payload $result) use ($pool) {
        echo $result['response'] . PHP_EOL;
        $pool->terminate();
    });

    echo 'tock' . PHP_EOL;

});
$loop->run();
factormaarten commented 6 years ago

Thanks for the response. This is what's happening:

I start the loop, it echo's tack, it echo's tock and after that it keeps waiting for $result['response']

use React\EventLoop\LoopInterface;
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
use function React\Promise\resolve;

final class ReturnChild implements ChildInterface
{
    public static function create(Messenger $messenger, LoopInterface $loop)
    {
        $messenger->registerRpc('executeQuery', function (Payload $payload) {
            return resolve([
                'response' => 'works'
            ]);
        });
    }
}

I don't see anything going wrong here. As I see it the promise should resolve.

WyriHaximus commented 6 years ago

With those edits above it works for me earlier this week: image

WyriHaximus commented 6 years ago

I'll set up a repo somewhere this weekend that has your code working

WyriHaximus commented 6 years ago

Hey @mpjraaij just set up a repository at https://github.com/WyriHaximus/shiny-octo-adventure run composer install and then php run.php and it (should) work. If it doesn't could you run strace php run.php > strace.txt 2>&1 and email strace.txt to the email address listed on my profile?

factormaarten commented 6 years ago

Thanks a lot! I'll go over it this week and see if it works :) if not I'll send you an email.

WyriHaximus commented 6 years ago

:+1:

cluigDE commented 4 years ago

Hi @WyriHaximus I tried to get the flexible pool to work by using a custom class. But every time I try the return-class example with a 100% identical copy of the ReturnChild class delivered by the messaging package, I get no resolve at all. (The only difference would be the name)

Also tried this on different environments: Debian, php7.0-7.4 OSX, php7.0-7.4 7.0 even breaks the whole project, which was kinda obvious.

eventloopserver.php

Flexible::createFromClass(LoopChildProcess::class, $loop)
->then(function (PoolInterface $pool) {
    echo 'tack', PHP_EOL;
    for ($i = 0; $i < 100; $i++) {
        //echo $i, PHP_EOL;

        $pool->rpc(
            MessagesFactory::rpc(
                'return',
                [
                    'i' => $i,
                    'time' => time(),
                    //'string' => str_pad('0', 1024 * 1024 * 5)
                    'string' => str_pad('0', 5)
                ]
            )
        )->then(function (Payload $payload) use ($pool) {
            echo $payload['i'], PHP_EOL;
            echo $payload['time'], PHP_EOL;
            if ($payload['i'] == 99) {
                $pool->terminate();
            }
            var_export($pool->info());
        });
    }
    echo 'tock', PHP_EOL;
});

$loop->run();

I also replaced the then()'s with done()'s, no error and actually just waiting for the promise resolve.

The LoopChildProcess has no namespace and is required the eventloopserver.php.

But the weird thing is everything works perfectly fine if I use:

Flexible::createFromClass('WyriHaximus\React\ChildProcess\Messenger\ReturnChild', $loop)

Anything I'm misunderstanding about the usage? Thanks for the help in advance.

WyriHaximus commented 4 years ago

hey @cluigDE silly question but is LoopChildProcess autoloaded?

cluigDE commented 4 years ago

No, the file containing the class was just included in the file eventloopserver.php

Didn't thought of autoloading the class in composer. Will do that. Thanks.