PHP Asynchronous Execution
Pure PHP process forking / --enable-pcntl
Async in pure PHP: How to Make Async Requests in PHP Speeding up PHP - Using process forking to accelerate image resizing · Oliver Smith
Gearman?
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
-
PHP: fastcgi_finish_request and finish tasks after that
session_write_close(); // close session file on server side to avoid blocking other requests fastcgi_finish_request();
Read comments: fastcgi_finish_request and session_write_close
ajax - fastcgi_finish_request creates hung connection when open session exists - Stack Overflow
Asynchronous operations in PHP and FPM
Gearman, perfectly explained:
All solutions:
Finally, I decided to use great feature of PHP-FPM:
Overview of PHP async technologies:
- All the PHP async resources: elazar/asynchronous-php
- Async processing or multitasking in PHP
- Run PHP Task Asynchronously
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.
Async cURL
I need both POST and GET request.
Hey: elazar/asynchronous-php: List of resources for asynchronous programming in PHP
-
Without library using fsockopen: Asynchronous HTTP requests for PHP This is async but opening a socket blocks for 300ms. And, that’s huge! Simple cURL forking is much faster, around 1ms in total. Source: How to Make Async Requests in PHP Note: it’s not working for me (open-ended request)
-
Smallest library: stil/curl-easy: cURL wrapper for PHP. Supports parallel and non-blocking requests. For high speed crawling, see stil/curl-robot only if you really need result: http - Asynchronous PHP calls? - Stack Overflow
-
Whole framework: Guzzle Documentation
-
cUrl with
ignore_user_abort();
: How to make asynchronous POST with PHP http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/
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
State in 2022
PHP 8.1 introduced Fibers i ovde su baš lepo objašnjeni
What is the parallelism in PHP and how do I use it?
It may surprise people to learn that the PHP standard library already has everything we need to write event-driven and non-blocking applications.
AMPHP as Asynchronous Multitasking PHP at repo amphp is working together with Revolt PHP
Davno sam koristio fastcgi_finish_request() a tu je i library: lstrojny/Procrastinator: Execute time consuming tasks as late as possible in a request