自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Webman如何使用Swow事件驅(qū)動(dòng)和協(xié)程?

開(kāi)發(fā) 前端
Swow 是一個(gè)專注于并發(fā) I/O 的跨平臺(tái)協(xié)程引擎,它致力于使用最小 C 核心及多數(shù) PHP 代碼以支持 PHP 高性能網(wǎng)絡(luò)編程,具有極佳的擴(kuò)展性與強(qiáng)大的調(diào)試能力,最大化開(kāi)發(fā)者的編程效率。

Swow 是什么?

Swow 是一個(gè)專注于并發(fā) I/O 的跨平臺(tái)協(xié)程引擎,它致力于使用最小 C 核心及多數(shù) PHP 代碼以支持 PHP 高性能網(wǎng)絡(luò)編程,具有極佳的擴(kuò)展性與強(qiáng)大的調(diào)試能力,最大化開(kāi)發(fā)者的編程效率。

安裝

安裝workerman 5.0

composer require workerman/workerman v5.0.0-beta.7

安裝日志

composer require workerman/workerman v5.0.0-beta.7
./composer.json has been updated
Running composer update workerman/workerman
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading workerman/workerman (v5.0.0-beta.1 => v5.0.0-beta.7)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading workerman/workerman (v5.0.0-beta.7)
  - Upgrading workerman/workerman (v5.0.0-beta.1 => v5.0.0-beta.7): Extracting archive
> support\Plugin::install
Generating autoload files
11 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.

安裝swow拓展

拉取源碼

composer require swow/swow

安裝日志

./composer.json has been updated
Running composer update swow/swow
Loading composer repositories with package information
Updating dependencies
Lock file operations: 5 installs, 0 updates, 0 removals
  - Locking composer/semver (3.4.2)
  - Locking psr/http-client (1.0.3)
  - Locking psr/http-factory (1.0.2)
  - Locking psr/http-message (2.0)
  - Locking swow/swow (v1.5.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing psr/http-message (2.0): Extracting archive
  - Installing psr/http-factory (1.0.2): Extracting archive
  - Installing psr/http-client (1.0.3): Extracting archive
  - Installing composer/semver (3.4.2): Extracting archive
  - Installing swow/swow (v1.5.3): Extracting archive
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
12 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
Using version ^1.5 for swow/swow

然后運(yùn)行 vendor/bin 目錄下的自動(dòng)安裝器 swow-builder 即可開(kāi)始安裝:

./vendor/bin/swow-builder

安裝日志

# ./vendor/bin/swow-builder
> cd /var/www/webman2024/vendor/swow/swow/ext && \
phpize && \
./configure 
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
...
Directive => Local Value => Master Value
swow.enable => On => On
swow.async_threads => 0 => 0
swow.async_file => On => On
swow.async_tty => On => On

?? Do you want to install it right now? (Y/n): Y
> cd /var/www/webman2024/vendor/swow/swow/ext && \
make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
? Install done

?????? All tasks have been completed ??????

出現(xiàn)以上結(jié)果,恭喜你安裝成功啦?。?!

開(kāi)啟swow事件驅(qū)動(dòng)

在webman框架中開(kāi)啟swow事件驅(qū)動(dòng)特別簡(jiǎn)單,只有配置一下就可以了。

修改配置文件config/server.php服務(wù)配置文件

return [
    'listen'           => 'http://0.0.0.0:8787',
    'transport'        => 'tcp',
    'context'          => [],
    'name'             => 'webman',
    'count'            => cpu_count() * 10,
    'user'             => '',
    'group'            => '',
    'reusePort'        => false,
    'event_loop'       => \Workerman\Events\Swow::class,
    'stop_timeout'     => 2,
    'pid_file'         => runtime_path() . '/webman.pid',
    'status_file'      => runtime_path() . '/webman.status',
    'stdout_file'      => runtime_path() . '/logs/stdout.log',
    'log_file'         => runtime_path() . '/logs/workerman.log',
    'max_package_size' => 10 * 1024 * 1024
];

啟動(dòng)webman,查看事件驅(qū)動(dòng)是否已經(jīng)是swow事件驅(qū)動(dòng)。使用以下命令啟動(dòng)

php -d extension=swow webman start

終端展示

/var/www/webman2024 # php -d extension=swow webman start
Workerman[webman] start in USER mode
-------------------------------------------------- WORKERMAN --------------------------------------------------
Workerman version:5.0.0-beta.7    PHP version:8.3.9     Event-loop:Workerman\Events\Swow
--------------------------------------------------- WORKERS ---------------------------------------------------
proto   user            worker                       listen                      processes    state            
tcp     root            webman                       http://0.0.0.0:8217         8             [OK]            
tcp     root            monitor                      none                        1             [OK]            
tcp     root            push_chart                   none                        1             [OK]            
tcp     root            plugin.webman.push.server    websocket://0.0.0.0:8788    1             [OK]            
---------------------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.

查看運(yùn)行狀態(tài)php start.php status

圖片圖片

協(xié)程

協(xié)程,英文Coroutines,是一種比線程更加輕量級(jí)的存在。最重要的是,協(xié)程不是被操作系統(tǒng)內(nèi)核所管理,而完全是由程序所控制(也就是在用戶態(tài)執(zhí)行)。

<?php
/**
 * @desc swow.php 描述信息
 * @author Tinywan(ShaoBo Wan)
 */
declare(strict_types=1);
use Exception;
use Swow\Coroutine;

$coroutine = Coroutine::run(static function (): void {
    while (true) {
        try {
            // output: int(999)
            var_dump(sleep(999));
        } catch (\Exception) {
            // skip
        }
    }
});
$coroutine->resume(); // resume the coroutine to cancel the sleep
$coroutine->throw(new Exception()); // throw exception to the coroutine
$coroutine->kill(); // kill the coroutine (just like SIGKILL)

debug模式

\Swow\Coroutine::run(static function (): void {
    (static function (): void {
        (static function (): void {
            var_dump(\Swow\Coroutine::getCurrent());
        })();
    })();
});

執(zhí)行輸出

/var/www/webman2024/app/controller/indexController.php:29:
class Swow\Coroutine#243 (5) {
  public $id =>
  int(13)
  public $state =>
  string(7) "running"
  public $switches =>
  int(0)
  public $elapsed =>
  string(3) "0ms"
  public $trace =>
  string(1252) "
#0 [internal function]: Swow\Coroutine->__debugInfo()
#1 /var/www/webman2024/app/controller/indexController.php(29): var_dump(Object(Swow\Coroutine))
#2 /var/www/webman2024/app/controller/indexController.php(30): app\controller\IndexController::app\controller\{closure}()
#3 /var/www/webman2024/app/controller/indexController.php(31): app\controller\IndexController::app\controller\{closure}()
#4 [internal function]: app\controller\IndexController::app\controller\{closure}()
#5 /var/www/webman2024/app/control"...
}
責(zé)任編輯:武曉燕 來(lái)源: 開(kāi)源技術(shù)小棧
相關(guān)推薦

2025-01-26 00:00:15

PHP協(xié)程控制權(quán)

2023-08-08 08:00:00

架構(gòu)Kafka

2023-12-13 09:56:13

?多進(jìn)程多線程協(xié)程

2024-10-18 10:27:50

PHP框架webma

2018-06-05 15:41:22

進(jìn)程線程協(xié)程

2023-06-21 08:00:00

微服務(wù)架構(gòu)

2023-12-17 14:24:46

計(jì)算機(jī)進(jìn)程線程

2024-10-22 15:34:57

2023-11-29 08:02:16

線程進(jìn)程

2023-10-31 22:54:17

GoEventBus驅(qū)動(dòng)編程

2024-08-27 12:49:20

2023-02-07 07:25:36

Spring事件驅(qū)動(dòng)

2021-01-28 11:17:49

Python爬蟲(chóng)單線程

2022-07-21 13:36:39

API事件驅(qū)動(dòng)Rest

2019-04-19 21:06:23

事件驅(qū)動(dòng)架構(gòu)VANTIQ

2015-05-29 13:59:53

2021-12-23 09:00:00

架構(gòu)微服務(wù)數(shù)據(jù)

2021-01-31 11:47:08

C語(yǔ)言SetjmpLongjmp

2009-06-25 14:05:08

Ajax JSF

2023-07-12 08:30:52

服務(wù)架構(gòu)事件驅(qū)動(dòng)架構(gòu)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)