PHPackages                             lruisen/think-worker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. lruisen/think-worker

ActiveLibrary[Queues &amp; Workers](/categories/queues)

lruisen/think-worker
====================

WorkerMan extend for ThinkPHP

2.0.0(1y ago)037Apache-2.0PHPPHP &gt;=8.0

Since Jul 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/lruisen/think-worker)[ Packagist](https://packagist.org/packages/lruisen/think-worker)[ RSS](/packages/lruisen-think-worker/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (8)Versions (10)Used By (0)

Thinkphp Workerman工程，让您的Thinkphp应用 `常驻内存` 运行！
---------------------------------------------

[](#thinkphp-workerman工程让您的thinkphp应用-常驻内存-运行)

特别鸣谢
----

[](#特别鸣谢)

> 站在巨人的肩膀上
>
> 排名不分先后

- [Workerman](https://www.workerman.net)
- [Webman](https://www.workerman.net/webman)
- [Thinkphp](https://www.thinkphp.cn)

运行环境
----

[](#运行环境)

- php &gt;= 8.0
- thinkphp &gt;= 8.0

介绍
--

[](#介绍)

- 相同的请求为php think run 耗时的N倍，快到起飞！
- 本模块依赖 `workerman`,`workerman/gateway-worker`,`workerman/crontab`，您可以使用它提供的方法，也可以直接使用 Workerman 4.x+ 的方法

安装扩展包
-----

[](#安装扩展包)

```
# 建议使用 composer 默认镜像，第三方镜像无法保证及时同步最新版本
composer require lruisen/think-worker
```

功能特性
----

[](#功能特性)

1. 文件监听热重载支持。
2. 本模块默认提供HTTP服务。
3. 提供基于 `workerman/crontab` 的定时任务服务
4. 提供基于 `topthink/think-queue` 的一键启动队列服务
5. 基于Workerman，就像Webman也是基于它一样「TP本身比Webman复杂」。
6. 支持使用docker容器运行「Dockerfile文件在源码根目录」。

一键启动全部服务
--------

[](#一键启动全部服务)

```
 # 此命令一键启动配置文件 `config/worker_*.php` 中 enable 为 true 的服务
 # 以下命令在linux中权限不足时请自行加sudo
 php think worker start #以调试模式启动服务
 php think worker start -d #以守护进程模式启动服务
 php think worker stop #停止服务
 php think worker restart #重启服务
 php think worker reload #柔性重启服务
 php think worker status #查看服务状态
```

独立启动HTTP服务
----------

[](#独立启动http服务)

- 服务配置文件位于`/config/worker_http.php`，默认端口为9501，所以一旦启动本服务，就不需要启动php think run了。
- `/config/worker_http.php` 配置文件中 `enable` 设置为 false 则不启动 http 服务
- 若您需要一些常驻内存运行程序的相关建议，请参考本介绍页的下半部分。

> 热更新配置在 `/config/worker_process/`中，APP\_DEBUG = true 模式下自动开启热更新

```
 # 此命令单独启动 Http 服务
 # 以下命令在linux中权限不足时请自行加sudo
 php think worker:http start #以调试模式启动服务
 php think worker:http start -d #以守护进程模式启动服务
 php think worker:http stop #停止服务
 php think worker:http restart #重启服务
 php think worker:http reload #柔性重启服务
 php think worker:http status #查看服务状态
```

独立启动队列服务
--------

[](#独立启动队列服务)

配置文件位于 `config/worker_process`以下配置代替think-queue里的最后一步:监听任务并执行,无需另外起进程执行队列

```
'queue' => [
    'enable' => false, // 是否开启队列监听并执行，true:开启，false:关闭
    'workers' => [
        // 在windows系统靠此处键值开启进程，此处键值作为进程名称
        'default' => [
            'count' => 1, // 监听队列任务的进程数
            'handler' => QueueHandle::class,
            'constructor' => [
                'options' => [
                    'name' => 'default',
                    'delay' => 0,
                    'sleep' => 3,
                    'tries' => 0,
                    'timeout' => 60,
                ]
            ]
        ],
    ],
],
```

> 具体配置参数请参考配置文件

```
 # 此命令单独启动 Queue 队列服务
 # 以下命令在linux中权限不足时请自行加sudo
 php think worker:queue start #以调试模式启动服务
 php think worker:queue start -d #以守护进程模式启动服务
 php think worker:queue stop #停止服务
 php think worker:queue restart #重启服务
 php think worker:queue reload #柔性重启服务
 php think worker:queue status #查看服务状态
```

独立启动定时任务服务
----------

[](#独立启动定时任务服务)

配置文件位于 `config/worker_cron`

> [基于workerman的定时任务程序crontab](https://www.workerman.net/doc/workerman/components/crontab.html)
>
> 具体配置参数请参考配置文件

```
 # 此命令单独启动 crontab 服务
 # 以下命令在linux中权限不足时请自行加sudo
 php think worker:cron start #以调试模式启动服务
 php think worker:cron start -d #以守护进程模式启动服务
 php think worker:cron stop #停止服务
 php think worker:cron restart #重启服务
 php think worker:cron reload #柔性重启服务
 php think worker:cron status #查看服务状态
```

支持`symfony/var-dumper`
----------------------

[](#支持symfonyvar-dumper)

> 中间件内容从 [top-think/think-swoole](https://github.com/top-think/think-swoole) 中摘抄而来

由于应用是通过php cli启动的，所以默认`symfony/var-dumper`会将调试信息打印在控制台, 通过配置中间件来支持将调试信息输出在网页上 如下是直接在配置在全局中间件上，也可以在路由定义的时候配置，建议是在路由定义的时候配置更加灵活

```
// app/middleware.php
