Multithreading in PHP

Why is not a good idea to use multithreading in php?

Does forking create a Thread ?

When we fork a process, the process space, that is to say the region of memory where the libraries and code the process requires to execute reside, is duplicated, the distinct but related processes then continue to execute at the will of the operating systems scheduler in different regions of memory.

What is the difference between a Forked Process and a Thread ?

When we create a Thread we are telling the operating system that we want another unit of execution that can operate in the same region of memory as the Process that created it.

How different operating systems actually implement threads and processes is beyond the scope of this answer, and is unimportant.

Why is Forking a bad idea at the frontend ?

When you copy the whole address space, you duplicate the region of memory that the webserver is operating in too, this can obviously cause havoc for your operating system.

Why is Threading a bad idea at the frontend ?

If a client script instructs the operating system to create 8 threads in direct response to a web request, and 100 clients simultaneously request the script, you will be instructing your operating system to execute 800 threads concurrently.

CPUs and operating systems would need to look very very different to make that a good idea!

Where is Threading a good idea?

Multi-threaded software, and extremely capable hardware, is ubiquitous; computing would not be what it is without it.

In the context of Web infrastructure, mysql and other database servers are multi-threaded, indeed Apache can deploy PHP in a multi-threaded infrastructure, though I wouldn't recommend it.

When we look at how enterprising applications like mysql actually provide their extremely complex services, we can see that their process (and therefore threads) are completely isolated from the infrastructure of your web application.

This is how we use Threads in languages that support them; we design systems whose means of providing their services is via some sane form of IPC, we isolate our complex infrastructure, completely, from that which should be simple: our web applications.

Is PHP really suitable for Threads ?

The memory model for PHP is shared nothing: this means that each interpreter context, in the sense of the structures and memory PHP requires to operate, is isolated from any other context.

This always has to be true for PHP to work as intended; an implementation of threading for PHP that was ignorant of the way PHP worked simply would not function.

pthreads goes to great lengths to ensure the memory model is not broken, every Thread does indeed not share memory directly with any other Thread.

Are Threads really suitable for me ?

Firstly, seriously think about the following questions:

  • Is Threading really required ?
  • What other ways can you find to achieve whatever it is you are setting out to do ?

Multi-threaded software is complex by nature; something being complicated is no kind of excuse for avoiding it, in my opinion.

But be aware that multi-threaded software is fundamentally different to your average PHP application, you have to think about things you have never had to think about before, be aware of things that didn't matter before you started your first Thread.

You should not guess at what these things are, you should seek to educate yourself in the subject as thoroughly as possible, and even be prepared to fail, and persevere.

The complexity of anything decreases as your knowledge increases, that's how learning works, here is where it begins:

https://gist.github.com/krakjoe/6437782

It continues in the manual, in the many examples distributed with pthreads, in stackoverflow searches and questions, and results in glory, in my opinion.

Multi Threading / Multi Tasking in PHP

PHP has had a threading model for a very long time, since the first release of PHP4, May 22nd 2000.

Threading at the frontend

Creating user threads at the frontend of a web application doesn't make any sense; it is extremely difficult to scale. The thread per client model that the Apache Worker MPM binary and mod_php employ is not really something you want to use to serve your websites, certainly if you are using it, you do not want to create additional threads in direct response to any web requests.

Why are threads at the frontend a bad idea ?

You may often hear developers say threads at the frontend do not make sense, without providing the rationale for such an assertion. When you learn to think about systems in the required way the problem becomes obvious:

If a client script creates 8 threads in direct response to a web request, and 100 clients request the script simultaneously, you are requesting that your hardware execute 800 threads concurrently.

CPU's would have to look and work very very differently indeed to make that a good idea

What can we do about it ?

Enterprising solutions might well have a PHP website facing the public, but the actual brains of the system are written in languages that have good support for those things you require to build enterprising solutions such as Java, C#, C++ or whatever the language-of-the-day is.

You should use pthreads in the same way; by designing systems whose component parts are separated from one another, only connected by well designed, high performance (RPC) API's, such that the complexity inherent in designing a multi-threaded architecture is isolated completely from your public facing websites, and the simple, scalable setup that such a website will require.

U can now haz codes

Let's start at the beginning with Hello World:

<?php
class My extends Thread {
public function run() {
printf("Hello World\n");
}
}

/* create a new Thread */
$my = new My();

/* start the Thread */
$my->start();

/* do not allow PHP to manage the shutdown of your Threads */
/* if a variable goes out of scope in PHP it is destroyed */
/* joining explicitly ensures integrity of the data contained in an objects */
/* members while other contexts may be accessing them */
$my->join();
?>

Boring, but I hope you read it ;)

So in a real system, you don't really want to be creating threads so explicitly, you surely want to just submit tasks to some executor service, all of the complex systems, in the sense of their multi-tasking requirements, I have ever seen use such things ...

<?php
class My extends Threaded {
public function run() {
printf("Hello World from %s#%lu\n",
__CLASS__, Thread::getCurrentThreadId());
}
}

/* create a Pool of four threads */
/* threads in a pool are created when required */
$pool = new Pool(4);

/* submit a few tasks to the pool */
$tasks = 100;
while ($tasks--) {
$pool->submit(new My());
}

/* shutting down the pool is tantamount to joining all workers */
/* remember what I said about joining ? */
$pool->shutdown();
?>

I have given you very brief explanations of complicated things, you should endeavor to read all you can:

  • https://gist.github.com/krakjoe/6437782
  • https://gist.github.com/krakjoe/9384409
  • http://php.net/pthreads

Many examples can be found here: https://github.com/krakjoe/pthreads/tree/master/examples

Disclaimer: There's nothing really wrong with a server architecture that uses threading, but the moment you start to create additional threads, you restrict it's scalability and ability to perform as it was designed, I can imagine well designed architectures that do have the ability to thread at the frontend, but it is not an easy thing to aim for. Additionally, threading is not the only thing in the toolbox when it comes to high performance web targeted applications; research all your options.

Does PHP have threading?

There is nothing available that I'm aware of. The next best thing would be to simply have one script execute another via CLI, but that's a bit rudimentary. Depending on what you are trying to do and how complex it is, this may or may not be an option.

backend multi-threading in PHP 7 (Symfony4)

There is pthreads extension that is rewritten to be much simpler in use in v3. It is supported on PHP 7.2+ and provides a way to create multi-threaded applications in PHP.

Alternatively since you're using Symfony - you can write simple console command that can use Process component to run sub-processes as separate OS processes. Here is example of such runner from actual project:

<?php

namespace App\Command;

use App\Command\Exception\StopCommandException;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Webmozart\PathUtil\Path;

class ProcessingRunner extends AbstractCommand
{
use LockableTrait;
/**
* @var Process[]
*/
private $processes = [];
/**
* @var string[]
*/
private $cmd;
/**
* @var KernelInterface
*/
private $kernel;

/**
* @param KernelInterface $kernel
*/
public function __construct(KernelInterface $kernel)
{
parent::__construct();
$this->kernel = $kernel;
}

/**
* {@inheritdoc}
* @throws InvalidArgumentException
*/
protected function configure(): void
{
$this
->setName('app:processing:runner')
->setDescription('Run processing into multiple threads')
->addOption('threads', 't', InputOption::VALUE_REQUIRED, 'Number of threads to run at once', 1)
->addOption('at-once', 'm', InputOption::VALUE_REQUIRED, 'Amount of items to process at once', 10);
}

/**
* {@inheritdoc}
* @throws \Symfony\Component\Process\Exception\LogicException
* @throws InvalidArgumentException
* @throws RuntimeException
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @throws \InvalidArgumentException
* @throws \LogicException
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
if (!$this->lock()) {
$output->writeln('The command is already running in another process.');
return 0;
}
if (extension_loaded('pcntl')) {
$stop = function () {
StopCommandException::throw();
};
pcntl_signal(SIGTERM, $stop);
pcntl_signal(SIGINT, $stop);
pcntl_async_signals(true);
}
do {
try {
while (\count($this->processes) < $this->getInput()->getOption('threads')) {
$process = $this->createProcess();
$process->start();
$this->processes[] = $process;
}
$this->processes = array_filter($this->processes, function (Process $p) {
return $p->isRunning();
});
usleep(1000);
} catch (StopCommandException $e) {
try {
defined('SIGKILL') || define('SIGKILL', 9);
array_map(function (Process $p) {
$p->signal(SIGKILL);
}, $this->processes);
} catch (\Throwable $e) {

}
break;
}
} while (true);
$this->release();
return 0;
}

/**
* @return Process
* @throws RuntimeException
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @throws \InvalidArgumentException
* @throws \LogicException
* @throws InvalidArgumentException
*/
private function createProcess(): Process
{
if (!$this->cmd) {
$phpBinaryPath = (new PhpExecutableFinder())->find();
$this->cmd = [
$phpBinaryPath,
'-f',
Path::makeAbsolute('bin/console', $this->kernel->getProjectDir()),
'--',
'app:processing:worker',
'-e',
$this->kernel->getEnvironment(),
'-m',
$this->getInput()->getOption('at-once'),
];
}
return new Process($this->cmd);
}
}

race condition in PHP using pthreads

Your assumption seems to be that your if condition followed by an immediate member assign always needs to run in one go. However, it is entirely possible that Friend runs this line of code in the thread:

if ($this->ingredient==null){

... and concludes to go ahead, but before it reaches the next line that assigns $this->ingredient, execution switches back to My/main thread, where it also gets to this line:

if ($this->ingredient==null){

And since Friend has passed the if but not proceeded to actually assigned the ingredient yet, My can now also pass inside. Whatever runs next doesn't matter, you now got both threads accessing the pot cooking at the same time.

Additional correction/note: it seems like that the example also doesn't work since $this->ingredient isn't a Volatile. However, that would still make it prone to above race condition and hence still a bad idea.

How to do it properly: You really need to use a mutex or synchronized section for proper synchronization. Also, never ever assume threads can't switch in the middle of anywhere, including any two lines like an if followed by a variable assign that was meant as a pair.

Here is the PHP documentation on the synchronized section: https://www.php.net/manual/en/threaded.synchronized.php



Related Topics



Leave a reply



Submit