Multi-threading is possible in php

Yes you can do multi-threading in PHP with pthreads

From the PHP documentation:

pthreads is an object-orientated API that provides all of the tools needed for multi-threading in PHP. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Threaded objects.

Warning: The pthreads extension cannot be used in a web server environment. Threading in PHP should therefore remain to CLI-based applications only.

Simple Test

#!/usr/bin/php
<?php
class AsyncOperation extends Thread {

    public function __construct($arg) {
        $this->arg = $arg;
    }

    public function run() {
        if ($this->arg) {
            $sleep = mt_rand(1, 10);
            printf('%s: %s  -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
            sleep($sleep);
            printf('%s: %s  -finish' . "\n", date("g:i:sa"), $this->arg);
        }
    }
}

// Create a array
$stack = array();

//Initiate Multiple Thread
foreach ( range("A", "D") as $i ) {
    $stack[] = new AsyncOperation($i);
}

// Start The Threads
foreach ( $stack as $t ) {
    $t->start();
}

?>

First Run

12:00:06pm:     A  -start -sleeps 5
12:00:06pm:     B  -start -sleeps 3
12:00:06pm:     C  -start -sleeps 10
12:00:06pm:     D  -start -sleeps 2
12:00:08pm:     D  -finish
12:00:09pm:     B  -finish
12:00:11pm:     A  -finish
12:00:16pm:     C  -finish

Second Run

12:01:36pm:     A  -start -sleeps 6
12:01:36pm:     B  -start -sleeps 1
12:01:36pm:     C  -start -sleeps 2
12:01:36pm:     D  -start -sleeps 1
12:01:37pm:     B  -finish
12:01:37pm:     D  -finish
12:01:38pm:     C  -finish
12:01:42pm:     A  -finish

Real World Example

error_reporting(E_ALL);
class AsyncWebRequest extends Thread {
    public $url;
    public $data;

    public function __construct($url) {
        $this->url = $url;
    }

    public function run() {
        if (($url = $this->url)) {
            /*
             * If a large amount of data is being requested, you might want to
             * fsockopen and read using usleep in between reads
             */
            $this->data = file_get_contents($url);
        } else
            printf("Thread #%lu was not provided a URL\n", $this->getThreadId());
    }
}

$t = microtime(true);
$g = new AsyncWebRequest(sprintf("http://www.google.com/?q=%s", rand() * 10));
/* starting synchronization */
if ($g->start()) {
    printf("Request took %f seconds to start ", microtime(true) - $t);
    while ( $g->isRunning() ) {
        echo ".";
        usleep(100);
    }
    if ($g->join()) {
        printf(" and %f seconds to finish receiving %d bytes\n", microtime(true) - $t, strlen($g->data));
    } else
        printf(" and %f seconds to finish, request failed\n", microtime(true) - $t);
}

多线程编程- PHP 实现- 枕边书- 博客园

https://www.cnblogs.com/zhenbianshu/p/7978835.html

2017年12月4日 ... 恰当地使用多线程能够大大提升程序效率,本文对比多进程介绍了下多线程的优势 和适用场景,提出了一种巧用方案,并使用PHP 代码实现了多 ...

How can one use multi threading in PHP applications - Stack Overflow

https://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications

Multi-threading is possible in php ... From the PHP documentation: pthreads is an object-orientated API that provides all of the tools needed for ...

php如何使用多线程? - 知乎

https://www.zhihu.com/question/31893506

(2)使用PHP实现一个Server,监听一个端口,为Web端提供服务。这里的实现 方式有很多,通常要配合扩展,例如原生的pthread(多线程),开源扩展swoole ...

通俗易懂的php多线程解决方案_w3cschool

https://www.w3cschool.cn/php/php-thread.html

2018年10月12日 ... ... 简单的交互。但是问题来了,我们都知道php本身是不支持多线程的,那么应该 怎么实现php多线程呢?_来自PHP 教程,w3cschool编程狮。

Facts Of PHP Multithreading That Will Make You Think Twice

https://www.clariontech.com/blog/facts-of-php-multithreading-that-will-make-you-think-twice

PHP applications, undoubtedly work effectively with multithreading capabilities. Multithreading is something similar to multitasking, but it enables to process ...

PHP到底能不能实现多线程? - 云+社区- 腾讯云

https://cloud.tencent.com/developer/article/1485905

2019年8月12日 ... 多线程能充分利用CPU,所以有多处大计算量代码时,也可以使用多线程使他们 并行执行,例如上文中后一个例子。 PHP中的多线程. PHP 默认并不 ...

php不支持多线程,所以不用考虑并发问题?这句话,对吗 ...

https://segmentfault.com/q/1010000005942449

2016年7月11日 ... PHP 语言代码本身(大部分情况下)是不关心自身是多进程还是多线程的。但,这并 不表示PHP 不支持多线程/多进程。php-fpm 就是多进程单线程 ...

Windows下PHP多线程扩展pthreads的安装_伪装者6的博客-CSDN博客

https://blog.csdn.net/qq_39896410/article/details/80019826

2018年4月20日 ... Windows下PHP多线程扩展pthreads的安装原创 2018年03月06日19:48:254查看 phpinfo() 扩展下载 ...

Introduction - Manual - PHP

https://www.php.net/manual/en/intro.pthreads.php

Introduction ¶. pthreads is an object-orientated API that provides all of the tools needed for multi-threading in PHP. PHP applications can create, read, write, ...

True PHP7 Multi-Threading: How to Rebuild PHP and use pthreads ...

https://rossbulat.medium.com/true-php7-multi-threading-how-to-rebuild-php-and-use-pthreads-bed4243c0561

Your PHP apps could really do with multi-threading capabilities for running tasks in parallel, but you know the PHP building process can be troublesome and ...

PHP多线程的实现方法详解-阿里云开发者社区

https://developer.aliyun.com/article/63436

2010年11月2日 ... 多线程是java中一个很不错的东西,很多朋友说在php中不可以使用PHP多线程了 ,其实那是错误的说法PHP多线程实现方法和fsockopen函数有关 ...

PHP里的多线程(pthreads)_风的专栏-CSDN博客_pthreads

https://blog.csdn.net/u010433704/article/details/92795346

2019年6月18日 ... 译者注:前阵子打算用到PHP的多线程, 但搜了一下, 才发现PHP多线程的资料少之 又少。尽管PHP官方文档里有讲到多线程,但是讲太少了,连多 ...

PHP多线程- 知乎

https://zhuanlan.zhihu.com/p/90713105

2019年11月8日 ... PHP在默认的情况下是不支持多线程的,通过安装pthreads扩展,让其参数来指定 编译PHP的线程安全方式来使其支持多线程,但是使用的过程中 ...

Parallel Programming with Pthreads in PHP - the Fundamentals ...

https://www.sitepoint.com/parallel-programming-pthreads-php-fundamentals/

Mar 22, 2017 ... It is not safe to use multiple threads in such an environment (causing IO issues, amongst other problems). It does not scale well. For example, let's ...

Asynchronous PHP — Multiprocessing, Multithreading & Coroutines ...

https://divinglaravel.com/asynchronous-php

Mar 26, 2021 ... Multiprocessing is an easy way to achieve asynchronous code execution in PHP. However, it's not the most performant. Because creating ...

PHP Multithreading - Thread Pool Example | Programster's Blog

https://blog.programster.org/php-multithreading-pool-example

When you start with multi-threading in PHP, you probably start by creating a Thread class and instantiating one for each of the tasks that you need doing. This is ...

Multithreading in PHP · Issue #460 · nginx/unit · GitHub

https://github.com/nginx/unit/issues/460

Closed due to technical issues on PHP's side. ... Multithreading in PHP #460. Closed. artemkonev opened this issue on Aug 14, 2020 · 0 comments. Closed ...

PHP Multithreading using pthreads extension - PHP Classes

https://www.phpclasses.org/blog/post/422-PHP-Multithreading-using-pthreads-extension.html

Pthreads is an object-oriented API which provides a convenient way to organize multi-threaded tasks in PHP. The API includes all the tools needed to create multi - ...