raphaeu/async

PHP 异步

安装: 36

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放性问题: 0

类型:项目

3.0.0.5 2020-10-18 18:58 UTC

This package is auto-updated.

Last update: 2024-09-19 03:17:35 UTC


README

Latest Stable Version Total Downloads Author License

使用 PHP 轻松高效地进行多任务异步请求。

要求

使用 composer 安装

composer require raphaeu/async

使用

// Import 
use async\Task;
use async\Async;

// Inicialize async class
$async = new Async();

// Create tasks
$task1 = new Task("php /tmp/randTimer.php");
$task2 = new Task("php /tmp/randTimer.php");
$task3 = new Task("php /tmp/randTimer.php");

// Vinculando testes
$async->addTask($task1);
$async->addTask($task2);
$async->addTask($task3);

// Rodando tarefas assíncronas
while ($totalOk = $async->hasDo())
{
    echo "Rodando: ".((count($async->getTasks()) - $totalOk) + 1)."/". count($async->getTasks()) ."\r";
    usleep(250);
}

// Pegando resultados das tarefas
foreach ($async->getTasks() as $task)
{
    echo PHP_EOL. $task->getResult();
}

异步测试文件 randTimer.php

<?php
  sleep(rand(1, 5));
  echo time();

配置

为了能够获取异步任务的结果,需要提供一个临时路径。

$async = new Async('/path-temp/');

变更日志

您可以在 GitHub Releases 页面找到变更列表。

问题解决

请将错误报告给 GitHub Issue Tracker

版权

本项目采用 MIT 许可证 许可。