Hacker News new | ask | show | jobs
by tgtweak 1523 days ago
Just be a real cowboy and use pcntl_fork()

Partially not kidding - I've used this as far back as 2004 to build cron jobs that pull a dataset, split it into N subsets, then fork out into N children to process those subsets of data in parallel. It's certainly not fluid like tasks and it creates a lot of memory duplication if you fork AFTER your data is pulled, but it works surprisingly well. Parent thread can wait for children to finish and even message children through socket pairs (or pcntl_signal for very basic stuff). I agree that it would be nice to have a native abstraction for tasks with messaging and concurrency limits built in.

1 comments

Fyi someone took pcntl_fork() in PHP seriously and made WorkerMan: https://github.com/walkor/workerman/blob/515ed41ac4fc51fc995...

A quick test got me 50k req/s HTTP "hello world" on my laptop.

The madlad even managed to place PHP in top 22 TechEmpower benchmark: https://i.imgur.com/6fKUpbV.png

It's surprisingly fast. I think they extended it to create copy-on-write memory at fork time, which made it incredibly fast and low-overhead to fork.