Skip to content

Instantly share code, notes, and snippets.

@krakjoe
krakjoe / crawler.php
Last active October 11, 2023 19:07
parallel Futures, Channels (buffered, unbuffered, synchros), Events using parallel producer/consumer pattern
<?php
use \parallel\{Runtime, Future, Channel, Events};
/* usage php crawler.php [http://example.com] [workers=8] [limit=500] */
$page = $argv[1] ?: "https://blog.krakjoe.ninja"; # 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
krakjoe / example.php
Last active October 11, 2023 19:07
Executor Service with Parallel and Channels
<?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
*/
@krakjoe
krakjoe / pthreads.md
Last active August 30, 2023 18:30
pthreads.md

Multi-Threading in PHP with pthreads

A Brief Introduction to Multi-Threading in PHP

  • Foreword
  • Execution
  • Sharing
  • Synchronization
  • Pitfalls
@krakjoe
krakjoe / document.php
Last active April 17, 2023 11:03
for generating stubs for a loaded extension
<?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
krakjoe / log.diff
Last active July 20, 2022 22:01
pcov vs xdebug comparison
--- 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
krakjoe / try-finally-auto-log
Created December 8, 2021 08:16
try-finally-auto
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
@krakjoe
krakjoe / php-xz.patch
Created August 19, 2019 13:41
php-xz php7
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
krakjoe / arrayof-perf.md
Last active September 11, 2020 17:05
arrayof-perf.md

Arrayof Performance

<?php
function arrayof_foo(Foo[] $fooze) {
    foreach ($fooze as $foo) {
    	$foo->bar();
    }
}
@krakjoe
krakjoe / test.md
Last active January 30, 2020 19:22

ui

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.

TODO

  • queue for pthreads
@krakjoe
krakjoe / pool.md
Last active January 30, 2020 12:33
Pooling

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.