PHP Asynchronous Execution

PHP Asynchronous Execution

Pure PHP process forking / --enable-pcntl

PHP: PCNTL - Manual

Async in pure PHP: How to Make Async Requests in PHP Speeding up PHP - Using process forking to accelerate image resizing · Oliver Smith

Gearman?

10up/WP-Gears · GitHub

Async PHP in Guzzle

Quickstart — Guzzle Documentation Using Composer, Laravel and Guzzle in your WordPress Plugin - Top Draw PHP HTTP Request With Guzzle - CodeSamplez

Non-blocking calls in PHP with GuzzleHttp REST API client - Stack Overflow

ReactPHP

React Users · reactphp/react Wiki · GitHub - bixuehujin/reactphp-mysql · GitHub - clue/php-block-react · GitHub - clue/php-packagist-api-react · GitHub

A(nother) new era of WordPress – Be the signal

Simple, async HTTP client for concurrently processing any number of HTTP requests, built on top of React PHP clue/php-buzz-react · GitHub


Just comparing: React + WordPress to create a high-speed non-blocking API in | Takahashi Fumiki .com Hack (HHVM) has also async as explained more here


New dd

Asynchronous operations in PHP and FPM

Gearman, perfectly explained:

Queues with Gearman and PHP

All solutions:

Asynchronous execution of PHP

Finally, I decided to use great feature of PHP-FPM:

Overview of PHP async technologies:

Didn’t use and why?

Guzzle HTTP?

Can I use guzzle to send asynchronous request without waiting the response?

You would have to do some pretty deep, custom handler code in order to “fire and forget” with Guzzle. Guzzle is designed to send complete requests, allowing us to know if the request actually succeeded.

You could try calling the *async() functions of the client to send requests and never call wait() on them. This would allow you to fire off a bunch of requests in your script, and when it shuts down, all of the pending requests will be waited upon.

If you want true async, then you’ll need to spin up a thread using something like pthreads, send requests off to a thread pool, hook up a custom handler and promise to work with this. It sounds pretty complicated and would take someone probably contributing a significant amount of refactoring code to Guzzle and guzzle/promises.

Process forking

Didn’t use forking a process (exec and similar) as I always disable those instructions: system,exec,passthru,shell_exec,proc_open as best security practice.

We can still use pcntl_fork: How to fork with PHP (4 different approaches)

Tutorial: Cross-platform background process forking without extensions in PHP How to fork with PHP (4 different approaches) php execute a background process

Don’t make mistake and avoid forking. You probably don’t need it. What you need is asynchronous task runner.

Parallel processing in PHP | David Müller

Task runner (Gearman)

Gearman is really great, but it does include extra daemon, multiple files and such. In the future, good thing to use.

Gearman In Your Sockets – Async PHP – Medium

Native Threads (pthreads)

pthreads is the way to go in PHP7, but you must recompile PHP with support for it.

Multithreading in PHP: Doing it right! krakjoe/pthreads pthreads - Manual

HHVM’s Async

HHVM has Async capability, but that also includes fixing HHVM compatibility issues in execution.

Asynchronous PHP We’re left with workarounds like ReactPHP, background forking of requests, curl_multi_exec() shenanigans and running worker-queues like Gearman.



WTF?

Welcome to asynchronous multitasking PHP ⋅ amphp Swoole: Event-driven asynchronous & concurrent networking engine with high performance for PHP

Async cURL

I need both POST and GET request.

Hey: elazar/asynchronous-php: List of resources for asynchronous programming in PHP

http - Asynchronous PHP calls? - Stack Overflow

Async Telegram

Is using Guzzle internally.

irazasyed/telegram-bot-sdk: Telegram 🤖 Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box. Asynchronous Requests

curl "https://api.telegram.org/bot301590951:AAHxw2lh2xPZVnydGeL1l9nZwyEDExW6Nrs/sendMessage?chat_id=@saveup_channel&text=Test"
Telegram UI Perks

There are groups, supergroups and channels. The only difference is in size, so we’ll be using groups. Any of them can be public or private.

We will use private groups, but they can’t be addressed by their name, but only by their id. And the process of getting an id is strange and involves a couple of steps: How to find out chat_id for a Group?

To add your bot to some group or chat, there will be no bots in contact list, so you need to search for it by entering your bot’s username.

The best explenation for Telegram is here and it contains example how to send data with Curl.

It is also quite strange how to obtain an API token. You need to create your bot, and you do that by using @BotFather, the one bot to rule them all. When you define your bot, you can obtain your API token, and with that token you send messages in the name of the bot.

What a xtzopwm

date 01. Jan 0001 | modified 28. May 2021
filename: PHP » Asynchronous