JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Implementing multithreading in PHP

Author:JIYIK Last Updated:2025/04/13 Views:

Multithreading is a form of program execution in which a single process creates multiple threads and they execute simultaneously. This tutorial will discuss multithreading in PHP and show you how to implement it.


ParallelImplementing multithreading in PHP using the Parallel Concurrency Extension

Using Parallelthe Parallel Concurrency Extension, we can implement multithreading in PHP.

This extension provides an interpreter thread parallel\Runtime. We can parallel\Runtime()create an object from the class, thereby creating a thread.

This class provides a method run(), which schedules tasks to run in parallel. We can pass Closureas a parameter to runthe method.

This parameter is generally called task, and we can also specify an array as the second parameter of the method. The contents of the array are passed to the task.

There are some requirements before downloading Parallelthe Parallel Concurrency Extension. The PHP version should be 8.0 and Zend Thread Safe (ZTS) should be enabled.

<pthread.h>The header is another requirement. We can pecldownload the extension from as shown below.

pecl install parallel

We can use forloops to test parallel execution of a program.

For example, we can run()run a loop inside a method and another loop outside that method. In this case, the code execution will be parallel.

For example, create parallel\Runtimean object of the class $rtand then use the object to call run()the method. In run()the method, write an anonymous function.

First, write a forloop to print the symbol 50 times inside the function +. Next, run()outside the method, write another forloop to print -the symbol 50 times.

Since run()the loop inside the method runs in a separate thread, run()the loop outside the method will be executed simultaneously. As a result, -the and +symbols are printed simultaneously, as shown in the output section below.

Therefore, we can use the Parallel Concurrency Extension to implement multithreading in PHP.

Sample code:

$rt = new \parallel\Runtime();

$rt->run(function(){
 for ($i = 0; $i < 50; $i++)
 echo "+";
});

for ($i = 0; $i < 50; $i++) {
 echo "-";
}

Output:

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

popen()Using functions to implement multithreading in PHP

We can popen()open parallel processes in PHP using the function.

This function forks the process to achieve parallel processing. The processes do not share resources.

In this way, we can implement multithreading in PHP. popen()The function creates a pipe to the forked process.

We can iterate over popen()the function and create multiple processes to implement multithreading. popen()The function takes commandas the first parameter and modeas the second parameter.

Mode can be either ror w.

For example, create a loop that forloops five times. Inside the loop, create another loop that loops five times for.

Inside the sub-loop, create an array $processto store popen()the functions. Set the PHP file message.phpand rmode as the first and second parameters.

Next, create another sub-loop and pclose()close it using the function $process.

Here, five processes are executed in parallel in the first sub-loop. The processes pclose()are terminated in the second sub-loop using the function.

This is how we use the function in PHP popen()to achieve multithreading.

Sample code:

for ($i=0; $i<5; $i++) {
 
 for ($j=0; $j<5; $j++) {
 $process[$j] = popen('message.php', 'r');
 }

 for ($j=0; $j<5; ++$j) {
 pclose($process[$j]);
 }
}

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial