Home > PHP Framework > ThinkPHP > This method can make TP5.0 fly on SWOOLE!

This method can make TP5.0 fly on SWOOLE!

藏色散人
Release: 2020-07-20 16:44:15
forward
3119 people have browsed it

The following tutorial column of thinkphp framework will introduce to you how to make TP5.0 fly on SWOOLE. I hope it will be helpful to friends who need it!

This method can make TP5.0 fly on SWOOLE!

TP-SWOOLE

Currently, TP5.1 has officially provided think-swoole2.0, which has a much more elegant level of integration. , but the integration method of 5.0 is indeed a bit tasteless. So I looked at 2.0 and developed an expansion package for 5.0. You can use composer to download it

composer require xaviertony/xavier-swoole
Copy after login

Before development, you need to be familiar with the life cycle of TP5.0, otherwise you will not be able to start.

Since TP mainly runs under Apache or NGINX, it will be released after each operation, while swoole is a permanent memory. Many TP5 classes are implemented by singletons, so it is inevitable to fall into pitfalls, among which the biggest pitfalls are the main ones. It is a request. Since the request is instantiated after startup, if the request force is not deleted, this instance will be used every time in the future, resulting in the inability to access the page normally, because the request instance needs to be deleted first after each request is reached

public static function deletethis()
    {
        if (!is_null(self::$instance)) {
            self::$instance=null;
        }
    }
Copy after login

The configuration file of the third-party package must be under application/extra, and the file name is swoole.php

<?php
return [
    &#39;host&#39;                  => &#39;0.0.0.0&#39;, // 监听地址
    &#39;port&#39;                  => 9501, // 监听端口
    &#39;mode&#39;                  => &#39;&#39;, // 运行模式 默认为SWOOLE_PROCESS
    &#39;sock_type&#39;             => &#39;&#39;, // sock type 默认为SWOOLE_SOCK_TCP
    &#39;app_path&#39;              => getcwd() . &#39;/application&#39;, // 应用地址 如果开启了 &#39;daemonize&#39;=>true 必须设置(使用绝对路径)
    &#39;file_monitor&#39;          => false, // 是否开启PHP文件更改监控(调试模式下自动开启)
    &#39;file_monitor_interval&#39; => 2, // 文件变化监控检测时间间隔(秒)
    &#39;file_monitor_path&#39;     => [], // 文件监控目录 默认监控application和config目录
    // 可以支持swoole的所有配置参数
    &#39;pid_file&#39;              => getcwd()  . &#39;/runtime/swoole.pid&#39;,
    &#39;log_file&#39;              => getcwd()  . &#39;/runtime/swoole.log&#39;,
    &#39;task_worker_num&#39;       => 20,
    //&#39;document_root&#39;         => getcwd() . &#39;public&#39;,
    //&#39;enable_static_handler&#39; => true,
    &#39;daemonize&#39;                => 1,//守护
    &#39;worker_num&#39; => 8,    //worker process num
    &#39;max_request&#39; => 10000,
];
Copy after login

Start command

php think swoole start
Copy after login

Daemon start

php think swoole start -d
Copy after login

Stop service

php think swoole stop
Copy after login

The above is the detailed content of This method can make TP5.0 fly on SWOOLE!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template