Cara menggunakan asynchronous php 8

Tahukah Anda bahwa Asynchronous HTTP client requests akan datang di Laravel 8.x. Lihat pada bulan Maret 2020, ketika Laravel 7  dirilis , klien HTTP baru diperkenalkan oleh Laravel yang pada dasarnya adalah pembungkus   klien HTTP Guzzle .

Anda telah melihat bahwa ini membuat pengalaman pengembang jauh lebih lancar dan lebih mudah. Misalnya, permintaan POST biasa menggunakan  klien HTTP  akan terlihat seperti ini

$response = :post['//test.com/users', [
    'name' => 'Ujang Kusnadi',
    'role' => 'Software Engineer',
]];

Concurrent Asynchronous Requests

Seperti yang saya sebutkan di atas,  PR  oleh  Andrea Marco Sartori  ini membawa konkurensi saat mengirim permintaan klien asynchronous dengan klien HTTP Laravel dengan menggunakan  Guzzle/Promises di  bawah tenda. PR ini akan disertakan dalam rilis Laravel 8.x berikutnya.

Pada dasarnya ada dua cara Anda dapat membuat permintaan asynchronous menggunakan ini.

  • Permintaan asynchronous menggunakan async[]
  • Kumpulan untuk menangani beberapa permintaan asynchronous secara bersamaan 

Asynchronous client requests menggunakan async[]

Jika Anda ingin membuat permintaan asynchronous, Anda dapat membuatnya seperti itu.

$promise = :async[]->get[$url];

Untuk membuat permintaan klien asynchronous, Anda perlu mengaitkan  async[] metode pada klien Http sebelum membuat permintaan. Tetapi alih-alih mengembalikan respons, itu akan mengembalikan    yang dapat Anda selesaikan/tolak seperti ini

$promise = :async[]->get[$url]->then[
    fn [Response|TransferException $result] => $this->handleResult[$result]
];

Dan ini adalah bagaimana kita bisa mendapatkan respons dari permintaan asynchronous.

Kumpulan untuk multiple async requests

Jika Anda ingin membuat beberapa permintaan asynchronous secara bersamaan, Anda dapat menggunakan kumpulan seperti itu.

A program does not need to be executed line by line. The program calls the function and moves further [does not wait for the function to return]. It will execute in the background. Non-Blocking methods are executed asynchronously. Async allows the parallel execution of the code. Async is also used to make faster execution of code and allow us the execution of multiple task.

 

Async Function in PHP:

  • In synchronous programming, if we want to read some file and return its content other functions block until the current function returns.
  • But an asynchronous programming program calls the function and moves further and all the same is happening in the background.
  • To write the Async program in PHP, we need to libraries [ReactPHP and Amp] which is used to implement non-blocking I/O in php. 
  • AMP is a concurrency framework for php and allows us to manage event loops, async iterators, and promises. 

Before going to the code, let’s first understand the functions provided by AMP:

1. then[] Function: The then[] function is executed only when the block of code in Async is successfully executed and we can perform operations on the result returned by Async.

Syntax:

then[function [] {
    // You can write operations here..
}]

2. timeout[] Function: The timeout[] function occurs when the php request takes longer time than usual and it does not need a parameter.

Syntax:

timeout[function [] {
    // Execute when request takes longer time to execute.
}]

3. catch Block: Exceptions are handled in the catch. When the program throws an exception[error] catch block will be executed.

Syntax:

catch[function [$exception] {
    // Here we can handle exceptions 
}]

4. await[] Function: The await function allows us to wait until async returns.

Syntax:

await[$promise];

5. Promise: A promise represents a single result of an asynchronous operation.

$promise = $deferred->promise[];

The steps required for “Async Code” are given below:

Step 1: You have to install composer to import libraries[ReactPHP and Amp].

Step 2: Initialize the composer using the following command.

composer init

Step 3: Create .php file. Your Project Structure should look like this.

 

Step 4: To implement the Async function we need to import the package first by using composer.

composer require amphp/react-adapter

Step 5: Import class present inside AMP.

PHP




Bài mới nhất

Chủ Đề