Symfony: PHP asynchronous or enqueue bundle, How to create a queue and progress bar
By: Azri Bilel
Tags: Enqueue, Redis, Symfony, PHP, Reactphp

What is Asynchronous Programming?

Asynchronous programs rely on non-blocking code to continuously process available tasks within a single thread of execution. Operations are only made with data that is immediately available. Since all data required by most programs cannot be immediately available, requests need to be made for data outside the program. Asynchronous programs must then use a different strategy for external operations that would normally cause the program to block. To avoid blocking, asynchronous programs use functions that also accept a callback function that is executed once the external request has been completed. Instead of blocking until the request is completed, the program is able to continue execution even though the result of the request is not available.

PHP scripts are generally written using blocking requests. For example, calling the function file_get_contents() to fetch the contents of a file will block the execution of the script until the operation has completed. While the process is blocked, no other code can be run within that PHP process.

To develop async PHP you must build the PHP package, because there are some functions not allowed in simple php (php-dev, php-common ..) like pcntl_async_signals(true).

Build PHP on Ubuntu

#!/usr/bin/env bash INSTALL_PKGS="libzip-dev bison autoconf build-essential pkg-config git-core libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev libpspell-dev libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev libfreetype6-dev libmysqlclient-dev libreadline-dev libcurl4-openssl-dev mysql-server apache2 libxslt-dev libpcre3 libpcre3-dev curl libenchant-dev ibcurl4-gnutls-dev" for i in $INSTALL_PKGS; do

sudo apt-get install -y $i

done ln -s x86_64-linux-gnu/curl /usr/include wget https://www.php.net/distributions/php-7.2.16.tar.gz -O /tmp/php7.2-lucky.tar.gz tar-C /tmp -xf /tmp/php7.2-lucky.tar.gz cd /tmp/php-7.2.16 /tmp/php-7.2.16/buildconf --force mkdir -p ~/bin/php7.2-lucky/ mkdir -p /etc/php7.2-lucky/conf CONFIGURE_STRING="--prefix=/etc/php7.2-lucky --sysconfdir=/etc --localstatedir=/var \

--datadir=/usr/share/php --mandir=/usr/share/man \

--with-bz2 --with-zlib --enable-zip --disable-cgi \

--enable-soap --enable-intl --with-openssl --with-readline --with-curl \

--enable-ftp --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \

--enable-sockets --enable-pcntl --with-pspell --with-enchant --with-gettext \

--with-gd --enable-exif --with-jpeg-dir --with-png-dir --with-freetype-dir --with-xsl \

--enable-bcmath --enable-mbstring --enable-calendar --enable-simplexml --enable-json \

--enable-hash --enable-session --enable-xml --enable-wddx --enable-opcache \

--with-pcre-regex --with-config-file-path=/etc/php7.2-lucky/conf \

--with-config-file-scan-dir=/etc/php7.2-lucky/etc --enable-cli --enable-maintainer-zts \

--with-tsrm-pthreads --enable-debug --enable-fpm --enable-phar --with-pear \

--with-fpm-user=www-data --with-fpm-group=www-data" --enable-sysvmsg /tmp/php-7.2.16/./configure $CONFIGURE_STRING make -C /tmp/php-7.2.16/

make -C /tmp/php-7.2.16/ install chmod o+x /etc/php7.2-lucky/bin/phpize

chmod o+x /etc/php7.2-lucky/bin/php-config git -C /tmp/php-7.2.16/ clone https://github.com/krakjoe/pthreads.git cd pthreads

/etc/php7.2-lucky/bin/phpize

./configure \

--prefix='/etc/php7.2-lucky' \

--with-libdir='/lib/x86_64-linux-gnu' \

--enable-pthreads=shared \

--with-php-config='/etc/php7.2-lucky/bin/php-config'

make -C /tmp/php-7.2.16/pthreads

make -C /tmp/php-7.2.16/pthreads install

cp /tmp/php-7.2.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

cp /tmp/php-7.2.16/sapi/fpm/php-fpm.conf /etc/php-fpm.conf

mkdir -p /etc/php-fpm.d/

cp /tmp/php-7.2.16/sapi/fpm/

chmod -R 755 /etc/init.d/php-fpm

update-rc.d php-fpm defaults ln -s /etc/php7.2-lucky/bin/* /usr/local/bin/cp /tmp/php-7.2.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmcp /tmp/php-7.2.16/sapi/fpm/php-fpm.conf /etc/php-fpm.confmkdir -p /etc/php-fpm.d/cp /tmp/php-7.2.16/sapi/fpm/ www.conf /etc/php-fpm.d/chmod -R 755 /etc/init.d/php-fpmupdate-rc.d php-fpm defaults cp /tmp/php-7.2.16/php.ini* /etc/php7.2-lucky/conf

cp /var/www/html/builder/php.ini /etc/php7.2-lucky/conf/php.ini cp /var/www/html/builder/php.ini /etc/php7.2-lucky/conf/php-cli.ini

echo "extension=pthreads.so" | sudo tee -a /etc/php7.2-lucky/conf/php-cli.ini

echo "zend_extension=opcache.so" | sudo tee -a /etc/php7.2-lucky/conf/php-cli.ini # if php build fail with icu error

# install icu for php intl

Notice: the PHP asynchronous work only on the CLI.

To make PHP scripts asynchronous you need to use one of that repositories: https://github.com/icicleio/icicle or: https://github.com/reactphp/react