jakubkulhan / bunny

Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library
MIT License
706 stars 102 forks source link

Async without ReactPHP? #133

Closed SerafimArts closed 1 year ago

SerafimArts commented 1 year ago

Hello! Thank you very much for the work done!

Can you please tell me if it is planned to add native PHP support? Would like to see something like:

// Client

public function receive(): Message
{
     do {
        \Fiber::getCurrent() && \Fiber::suspend();
        $message = $this->pollMessage(); // Message|null
     } while ($message !== null);

    return $message;
}
// Sync
$message = $channel->receive();

// Async
$first = new \Fiber($channel1->receive(...));
$second = new \Fiber($channel2->receive(...));

// ... process fibers (using "while(!$fiber->isTerminated())" + "$fiber->resume()")

$first->getReturn();
$second->getReturn();
WyriHaximus commented 1 year ago

Hey @SerafimArts, the current plan is combined both clients into one still with ReactPHP as backend and exposing a fiber based API that doesn't expose any promises.

SerafimArts commented 1 year ago

ReactPHP does not provide the ability to manually control the event loop.

Like:

while ($loop->isRunning()) {
    $loop->tick();

    // Do something else not related to the loop
}

In any case, thanks for the reply! I will think about how to solve the problem =(

P.S. Those. the problem is not that there are Promises, but that in the current version I cannot control the event loop by myself.

WyriHaximus commented 1 year ago

Well what is the problem that you are trying to solve by controlling the event loop?

SerafimArts commented 1 year ago

The absence of a global breakpoint (i.e. the physical absence of a while(true)).

Well, the physical absence of the event loop, too, because. in fact, it is simply not needed =)))

WyriHaximus commented 1 year ago

While we officially don't recommend it, you can do something like this to control the event loop:


while(true) {
    Loop::addTimer(0.01, static function (): void {Loop::stop()});
    Loop::run();
}
SerafimArts commented 1 year ago

It's too strong looks like a perversion)))

WyriHaximus commented 1 year ago

It is :joy: