Skip to content

Commit

Permalink
Update EventLoop and Coroutine!
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Aug 13, 2024
1 parent 4c66b58 commit b3bc87e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/vennv/vapm/simultaneous/CoroutineGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,20 @@ private static function run(): void
$coroutine = self::$taskQueue->dequeue();

if ($coroutine instanceof ChildCoroutine) {
if ($i++ >= 5) $fnWait($i);
$coroutine->run();
if (!$coroutine->isFinished()) self::schedule($coroutine);
if ($i >= 5) $fnWait($i);
}

if ($coroutine instanceof CoroutineScope) {
Async::await($coroutine->run());
if (!$coroutine->isFinished()) self::schedule($coroutine);
}
$i++;
}
} catch (Throwable $e) {
echo $e->getMessage();
}
});
}

}
}
10 changes: 5 additions & 5 deletions src/vennv/vapm/simultaneous/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public static function getReturns(): Generator;
class EventLoop implements EventLoopInterface
{

protected static int $limit = 10;

protected static int $nextId = 0;

/**
Expand Down Expand Up @@ -175,13 +177,12 @@ protected static function run(): void
if (count(GreenThread::getFibers()) > 0) GreenThread::run();

$i = 0;
$limit = min((int)((count(self::$queues) / 2) + 1), 100); // Limit 100 promises per loop


/**
* @var Promise $promise
*/
foreach (self::getQueues() as $promise) {
if ($i >= $limit) break;
if ($i++ >= self::$limit) break;

$id = $promise->getId();
$fiber = $promise->getFiber();
Expand All @@ -204,8 +205,6 @@ protected static function run(): void
self::$queues->detach($promise); // Remove from queue
self::$queues->attach($promise, $id); // Add to queue again
}

$i++;
}

if (count(MicroTask::getTasks()) > 0) MicroTask::run();
Expand All @@ -219,6 +218,7 @@ protected static function run(): void
*/
protected static function runSingle(): void
{
self::$limit = min((int)((count(self::$queues) / 2) + 1), 100); // Limit 100 promises per loop
while (count(self::$queues) > 0 || count(MicroTask::getTasks()) > 0 || count(MacroTask::getTasks()) > 0 || count(GreenThread::getFibers()) > 0) self::run();
}

Expand Down

0 comments on commit b3bc87e

Please sign in to comment.