PHP Asynchronous Execution
Pure PHP process forking /
Async PHP in Guzzle
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
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();
Gearman, perfectly explained:
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?
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.
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)
Don’t make mistake and avoid forking. You probably don’t need it. What you need is asynchronous task runner.
Task runner (Gearman)
Gearman is really great, but it does include extra daemon, multiple files and such. In the future, good thing to use.
Native Threads (pthreads)
pthreads is the way to go in PHP7, but you must recompile PHP with support for it.
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.
I need both POST and GET request.
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
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/
Is using Guzzle internally.
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