Skip to content

Instantly share code, notes, and snippets.


Multi-Threading in PHP with pthreads

A Brief Introduction to Multi-Threading in PHP

  • Foreword
  • Execution
  • Sharing
  • Synchronization
  • Pitfalls
krakjoe / crawler.php
Last active Aug 16, 2022
parallel Futures, Channels (buffered, unbuffered, synchros), Events using parallel producer/consumer pattern
View crawler.php
use \parallel\{Runtime, Future, Channel, Events};
/* usage php crawler.php [] [workers=8] [limit=500] */
$page = $argv[1] ?: ""; # start crawling this page
$workers = $argv[2] ?: 8; # start this number of threads
$limit = $argv[3] ?: 500; # stop at this number of unique pages
$timeout = $argv[4] ?: 3; # socket timeout for producers
krakjoe / log.diff
Last active Jul 20, 2022
pcov vs xdebug comparison
View log.diff
--- pcov.log 2019-01-20 14:28:58.444851609 +0100
+++ xdebug.log 2019-01-20 14:25:27.607884120 +0100
@@ -1,8 +1,8 @@
-time php vendor/bin/phpunit --coverage-text --colors=never 2>&1 > pcov.log
+time php vendor/bin/phpunit --coverage-text --colors=never 2>&1 >xdebug.log
PHPUnit 7.5.2 by Sebastian Bergmann and contributors.
-Runtime: PHP 7.2.15-dev
+Runtime: PHP 7.2.15-dev with Xdebug 2.7.0beta2-dev
krakjoe / document.php
Last active Apr 8, 2022
for generating stubs for a loaded extension
View document.php
usage: php document.php --ext name [--output document.txt]
function prototype(Reflector $reflector) {
$elements = [];
switch (get_class($reflector)) {
case "ReflectionClass":
if ($reflector->isFinal()) {
krakjoe / example.php
Last active Apr 8, 2022
Executor Service with Parallel and Channels
View example.php
use \parallel\{Runtime, Channel};
class ExecutorService {
public function __construct(int $workers, string $channel = __CLASS__, int $backlog = Channel::Infinite) {
if ($backlog == 0) {
* execute() will block until a worker is ready
View try-finally-auto-log
string(12) "Oh Exception"
Finally Foo::bar
Finally Foo::qux
Fatal error: Uncaught RuntimeException: Oh qux! in /opt/src/php-src/try-finally-auto.php:22
Stack trace:
#0 /opt/src/php-src/try-finally-auto.php(30): Foo->qux()
#1 {main}
thrown in /opt/src/php-src/try-finally-auto.php on line 22
View php-xz.patch
diff --git a/config.m4 b/config.m4
index 90bbe66..e3ffe7c 100644
--- a/config.m4
+++ b/config.m4
@@ -1,47 +1,10 @@
dnl $Id$
dnl config.m4 for extension xz
-dnl Comments in this file start with the string 'dnl'.
-dnl Remove where necessary. This file will not work
krakjoe /
Last active Sep 11, 2020

Arrayof Performance

function arrayof_foo(Foo[] $fooze) {
    foreach ($fooze as $foo) {


Build Status

This extension wraps the very excellent libui to provide PHP 7 with an API for the creation of cross platform native look-and-feel interfaces.


  • queue for pthreads

Easy pthreads Pools

The final solution !!

Since the first version of pthreads, PHP has had the ability to initialize Worker threads for users. Onto those Worker threads are stacked objects of class Stackable for execution concurrently.

The objects stacked onto workers do not have their reference counts changed, pthreads forces the user to maintain the reference counts in userland, for the extremely good reason that this enables the programmer to keep control of memory usage; and so, execute indefinitely.

This is the cause of much heartache for newcomers to pthreads; if you do not maintain references properly you will, definitely, experience segmentation faults.