swoole 2 basic concepts and long connections

1. Synchronous and asynchronous

For the execution process of the program, it is generally executed from top to bottom, unless there will be some changes in the process control statement, but this principle will be followed in principle; For PHP, in single thread mode, a process will execute from the first line when executing PHP code
From the row to the last row, we can think that this process is synchronous. If sleep() is encountered in the middle, we have to wait for execution; This mode can basically meet many application scenarios;
But it is not absolute. For example, when wechat logs in on the website, it will send a message to remind you to log in on its wechat account. For another example, after an order is generated, the user will not receive the goods immediately, and we can make asynchronous tasks in the process of delivery
Inventory module processing and other operations;

2. Blocking and non blocking

Blocking: it means where to wait and wait for others to complete the implementation before going down;
Non blocking: that is, the program can proceed to the next step without waiting for the execution result;
So in this process, for order.php, after we enter a task into the console
The program is waiting for the input of the next information of the task, so we can think of this process as synchronous blocking; However, for inventory.php, it does not need to wait for order.php to execute



For its execution, it needs to see whether there are tasks to be completed in task.txt. At this time, it is asynchronous and non blocking compared with order.php;
The exclusive names for blocking and non blocking are synchronous blocking, synchronous non blocking, asynchronous blocking, asynchronous non blocking; In fact, we don't need to pay attention to synchronous non blocking and asynchronous blocking. There will be some complexity in the implementation of synchronous non blocking, which can be cleverly implemented by asynchronous non blocking
Solve the problem; Asynchronous blocking is not meaningful in itself

3. Asynchronous callback in swoole

This asynchronous callback module exists in the swoole - http://wiki.swoole.com/wiki/page/p-async.html However, for this callback module, in version 4.3, all asynchronous client modules have been migrated. In the EXT async extension, it is recommended to use the collaboration client;
Of course, we can download the extension from GitHub for compilation and installation https://github.com/swoole/ext-async , I downloaded the zip package here

[root@localhost file]# ls ext-async-master.zip swoole-src-4.4.12 swoole-src-4.4.12.tar.gz
[root@localhost file]# unzip ext-async-master.zip
[root@localhost file]# ls ext-async-master ext-async-master.zip swoole-src-4.4.12 swoole-src-4.4.12.tar.gz
[root@localhost file]# cd ext-async-master
[root@localhost ext-async-master]# phpize checking target system type... x86_64-unknown-linux-gnu configure: error: Cannot find php-config. Please use --with-php-config=PATH
[root@localhost ext-async-master]# find / -name php-config /www/server/php/72/src/scripts/php-config /www/server/php/72/bin/php-config [root@localhost ext-async-master]# ./configure --with-php-config=/www/server/php/72/bin/php-config [root@localhost ext-async-master]# make -j 4
[root@localhost ext-async-master]# sudo make install Installing shared extensions: /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/
[root@localhost ext-async-master]# find / -name php.ini /www/server/php/72/etc/php.ini
[root@localhost ext-async-master]# vi /www/server/php/72/etc/php.ini add to extension=swoole_async.so
[root@localhost ext-async-master]# /etc/rc.d/init.d/php-fpm-72 restart
[root@localhost ext-async-master]# php -m | grep swoole swoole swoole_async



Posted by colmtourque on Thu, 11 Nov 2021 13:25:10 -0800