This is a short sub-section with a couple of chapters dealing with the processing and modification of data stored on the system as a result of an event that occurs at a point in time not associated with the entry or the display of the data.
Whilst it is beyond the scope of this documentation to discuss, it should be noted that PHP can and does support multi-threading and therefore concurrency or asynchronous behaviour.
How does asynchronous programming differ from synchronous programming?
In synchronous programming
We issue a statement,
We wait for it to complete
..and then move on to the next statement.
In asynchronous programming
We issue a statement
While we are waiting for it to complete, we can perform other tasks
When it has finished, we run a callback function
This means we can reduce total execution time using an asynchronous methodology because we minimize the time spent waiting for operations to complete.
The downside is that often callback depend on callbacks which depend on other callbacks
How can we avoid deeply nested callbacks (Callback Hell)?
- We don't have to use callbacks:
- async/await in Hack and C#
- Threads in Java
- Use libraries like cURL or Guzzle to help
- Promises (or 'futures') can help
- Promises don't avoid nesting callbacks - they just provide a nicer syntax to deal with them
What is the difference between multi-threading and asynchronous programming?
- Usually asynchronous code/languages will use threads
- This is an implementation detail which is abstracted from the user
- Usually in asynchronous code, all of the core application logic takes place in one thread
- Other threads are usually 'lightweight' and are just waiting for some blocking/IO code to complete
- In true multithreaded programming, core program logic will be executed in many threads
Introducing Asynchronous concepts into PHP
- This is unlike the 'traditional' implementation/libraries we are used to in PHP
- We can use multi-threading in PHP using the pthreads extension
- This requires Zend Thread Safety
- Threads are not necessary for concurrent execution in PHP - Process Control Extensions use process forking
- If we are to write PHP asynchronously, this rules out use of a lot of functions/libraries we like to use, because they block on IO
- These problems are not unique to PHP
Libraries for writing async PHP
- React PHP
- On a simple level, it is possible to write asynchronous PHP code by spawning additional processes using exec - you don't need a library
PHP 7 supports simultaneous execution of asynchronous tasks. With PHP 7, you can do asynchronous programming and tasks such as, networks, access to database, timers and perform events related I/O operations by putting in place a single PHP event-loop. By the way, the Facebook Hack language for HHVM already supports asynchronous programming. PHP-based servers can also handle simultaneous requests from a single memory pool because PHP 7 supports standalone multi-threading.
Created with the Personal Edition of HelpNDoc: Full-featured Documentation generator