dmamontov / asynctask-7

AsyncTask 允许正确且简单地使用线程。此类允许在无需操作线程和/或处理程序的情况下执行后台操作并发布结果。

2.0.13 2019-02-07 07:17 UTC

This package is auto-updated.

Last update: 2024-08-29 05:26:30 UTC


README

Build Status Latest Stable Version License Total Downloads PHP Classes

AsyncTask

AsyncTask 允许正确且简单地使用线程。此类允许在无需操作线程和/或处理程序的情况下执行后台操作并发布结果。

早期类实现支持PHP 5.3,但不支持此类中实现的功能。

要求

  • PHP 版本 ~7.1.0

  • 已安装模块 pcntlposix

  • 从指令 disable_functions 中移除所有 pcntlposix 函数

  • 共享内存适配器:

    • 从指令 disable_functions 中移除所有 shm 函数

安装

  1. 安装 composer

  2. 在项目文件夹中执行

composer require dmamontov/asynctask-7 ~2.0.13

config/composer.json 中,您的项目将被添加到库 dmamontov/asynctask-7 中,该库位于 vendor/ 文件夹中。如果没有配置文件或供应商文件夹,它们将被创建。

如果之前您的项目没有使用 composer,则连接启动文件供应商。为此,在项目中输入以下代码

require 'path/to/vendor/autoload.php';

适配器列表

  • SharedMemory - 正在工作
  • FileSystem - 在过程中
  • Redis - 在过程中

提供缺少的适配器。我们正在开发中!

示例

工作示例

use AsyncTask\{
    AsyncTask,
    Collection
};

class TestTask extends AsyncTask
{
    protected function onPreExecute(Collection $collection)
    {
    }

    protected function doInBackground(Collection $collection)
    {
        return 'My First Task';
    }

    protected function onPostExecute($result)
    {
        echo $result;
    }

    protected function publishProgress()
    {
        echo rand(0,9) . PHP_EOL;
    }
}

$task = new TestTask();
$task
    ->setTitle('TestTask')
    ->execute(new Collection);

任务示例

use AsyncTask\AsyncTask;
use AsyncTask\Collection;

class ExampleTask extends AsyncTask
{
    /**
     * Optional method.
     */
    protected function onPreExecute(Collection $collection)
    {
        return $collection;
    }

    /**
     * Required method.
     */
    protected function doInBackground(Collection $collection)
    {
        return $collection;
    }

    /**
     * Optional method.
     * With this method, an additional process is created.
     */
    protected function publishProgress()
    {
    }

    /**
     * Optional method.
     */
    protected function onPostExecute($result)
    {
    }

    /**
     * Optional method.
     */
    protected function onCancelled()
    {
    }
}

适配器示例

use AsyncTask\Adapter;
use AsyncTask\Interfaces\AdapterInterface;

class ExampleAdapter extends Adapter implements AdapterInterface
{

    /**
     * Required method.
     */
    public function init(): AdapterInterface
    {
        return $this;
    }
    
    /**
     * Required method.
     */
    public function finish(): AdapterInterface
    {
        return $this;
    }

    /**
     * Required method.
     */
    public function clean(bool $parent = false): AdapterInterface
    {
        return $this;
    }
    
    /**
     * Required method.
     */
    public function has($key, bool $parent = false): bool
    {
        return false;
    }

    /**
     * Required method.
     */
    public function remove($key, bool $parent = false): bool
    {
        return true;
    }
    
    /**
     * Required method.
     */
    public function get($key, bool $parent = false)
    {
        return null;
    }

    /**
     * Required method.
     */
    public function write($key, $val, bool $parent = false): AdapterInterface
    {
        return $this;
    }
}

待办事项

  • 更多测试。
  • 更多适配器。
  • 用于管理运行进程的类。