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);
}
2017年12月4日 ... 恰当地使用多线程能够大大提升程序效率,本文对比多进程介绍了下多线程的优势 和适用场景,提出了一种巧用方案,并使用PHP 代码实现了多 ...
Multi-threading is possible in php ... From the PHP documentation: pthreads is an object-orientated API that provides all of the tools needed for ...
(2)使用PHP实现一个Server,监听一个端口,为Web端提供服务。这里的实现 方式有很多,通常要配合扩展,例如原生的pthread(多线程),开源扩展swoole ...
2018年10月12日 ... ... 简单的交互。但是问题来了,我们都知道php本身是不支持多线程的,那么应该 怎么实现php的多线程呢?_来自PHP 教程,w3cschool编程狮。
PHP applications, undoubtedly work effectively with multithreading capabilities. Multithreading is something similar to multitasking, but it enables to process ...
2019年8月12日 ... 多线程能充分利用CPU,所以有多处大计算量代码时,也可以使用多线程使他们 并行执行,例如上文中后一个例子。 PHP中的多线程. PHP 默认并不 ...
2016年7月11日 ... PHP 语言代码本身(大部分情况下)是不关心自身是多进程还是多线程的。但,这并 不表示PHP 不支持多线程/多进程。php-fpm 就是多进程单线程 ...
2018年4月20日 ... Windows下PHP多线程扩展pthreads的安装原创 2018年03月06日19:48:254查看 phpinfo() 扩展下载 ...
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, ...
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 ...
2010年11月2日 ... 多线程是java中一个很不错的东西,很多朋友说在php中不可以使用PHP多线程了 ,其实那是错误的说法PHP多线程实现方法和fsockopen函数有关 ...
2019年6月18日 ... 译者注:前阵子打算用到PHP的多线程, 但搜了一下, 才发现PHP多线程的资料少之 又少。尽管PHP官方文档里有讲到多线程,但是讲太少了,连多 ...
2019年11月8日 ... PHP在默认的情况下是不支持多线程的,通过安装pthreads扩展,让其参数来指定 编译PHP的线程安全方式来使其支持多线程,但是使用的过程中 ...
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 ...
Mar 26, 2021 ... Multiprocessing is an easy way to achieve asynchronous code execution in PHP. However, it's not the most performant. Because creating ...
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 ...
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 ...
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 - ...