# Multithreading

- Thread: https://externals.io/message/21086
- Original archive: https://news-web.php.net/php.internals/21086
- Started by Bart de Boer on 2005-12-08T00:46:30+00:00
- Messages: 19
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21086 — Bart de Boer — 2005-12-08T00:46:30+00:00

URL: https://externals.io/message/21086#21086

Dear PHP creators,

I was wondering. (And I'm probably the 1000th person asking this.) With 
all the multiple-core processors and servers popping up lately, are 
there any future (or current) plans for multithreading in PHP?

It's not unlikely to have a PHP script that, for example, needs to send 
an email, do some database transactions and perhaps communnicate with a 
web service simultaneously. In such situations where the script needs to 
wait for multiple external actions it could really improve performance.

I know there are other ways to do it. (It's possible to do 
"multithreading" with sockets.) Still I think it would be cool if there 
would be native support. As a fanatic PHP programmer it has always been 
something that I envy about in other languages.

Bart

---

## #21087 — Rasmus Lerdorf — 2005-12-08T00:49:07+00:00

URL: https://externals.io/message/21086#21087 · In reply to: #21086

Bart de Boer wrote:
> Dear PHP creators,
> 
> I was wondering. (And I'm probably the 1000th person asking this.) With 
> all the multiple-core processors and servers popping up lately, are 
> there any future (or current) plans for multithreading in PHP?
> 
> It's not unlikely to have a PHP script that, for example, needs to send 
> an email, do some database transactions and perhaps communnicate with a 
> web service simultaneously. In such situations where the script needs to 
> wait for multiple external actions it could really improve performance.
> 
> I know there are other ways to do it. (It's possible to do 
> "multithreading" with sockets.) Still I think it would be cool if there 
> would be native support. As a fanatic PHP programmer it has always been 
> something that I envy about in other languages.

There are no plans for this.  Asynchronous mechanisms are generally a 
much more efficient way to do this stuff.

-Rasmus

---

## #21088 — Ilia Alshanetsky — 2005-12-08T00:54:32+00:00

URL: https://externals.io/message/21086#21088 · In reply to: #21086

You don't need threads, you can use fork() for that. On *nix systems it
is very fast, nearly as fast as threads and much safer to boot. And you
can already do forking in PHP via PCNTL extension's `pcntl_fork()` function.

Ilia

---

## #21089 — Sara Golemon — 2005-12-08T01:22:28+00:00

URL: https://externals.io/message/21086#21089 · In reply to: #21086

> I was wondering. (And I'm probably the 1000th person asking this.) With
> all the multiple-core processors and servers popping up lately, are
> there any future (or current) plans for multithreading in PHP?
>
I've got two...well...threads... going on this concept.... One is a very
non-true-threading apprach which involves a combination of ticks and
Runkit_Sandbox which I hope to get around to sooner rather than later.

The other is a longer-range plan designed to work specifically with
apache2-worker (though perhaps generalized to work with other threaded
SAPIs) to actually use some of those extra threads to link multiple real
interpreters together (allowing one request chain to start/shutdown another
in a sibling relationship -- as opposed to Runkit_Sandbox which is purely
parent/child).  Don't look for this anytime soon though.  It's in my
"wouldn't that be kinda cool" bin of projects to work on next to oggtheora
and a few unnamed beasts...

-Sara

---

## #21090 — Bart de Boer — 2005-12-08T01:31:45+00:00

URL: https://externals.io/message/21086#21090 · In reply to: #21087

Rasmus Lerdorf wrote:
> Bart de Boer wrote:
> > […]
> 
> There are no plans for this.  Asynchronous mechanisms are generally a 
> much more efficient way to do this stuff.
> 
> -Rasmus

With asynchronous I'm asuming you mean mechanisms where you call other 
scripts (web services) from within the main script? Besides the extra 
overhead, scripts and code, I guess this is a good alternative.

The thing is. I'm currently working on a project that I'm forced to do 
with *cough* M$ C# *cough*. I hate everything about it except for the 
threading. Which is... not so bad. :) With just a teeny bit of extra 
code I'm able to call some functions or methods in seporate threads and 
wait untill they're done.

Just think it would be nice... :)

---

## #21091 — Bart de Boer — 2005-12-08T01:31:57+00:00

URL: https://externals.io/message/21086#21091 · In reply to: #21088

Ilia Alshanetsky wrote:
> You don't need threads, you can use fork() for that. On *nix systems it
> is very fast, nearly as fast as threads and much safer to boot. And you
> can already do forking in PHP via PCNTL extension's `pcntl_fork()` function.
> 
> Ilia

That sounds sufficient enough. Although I guess this only works on *nix 
systems? Platform independence is one of the main reasons I prefer PHP 
over other languages.

---

## #21092 — Bart de Boer — 2005-12-08T01:37:13+00:00

URL: https://externals.io/message/21086#21092 · In reply to: #21089

Sara Golemon wrote:

>I've got two...well...threads... going on this concept.... One is a very
>non-true-threading apprach which involves a combination of ticks and
>Runkit_Sandbox which I hope to get around to sooner rather than later.
>
>The other is a longer-range plan designed to work specifically with
>apache2-worker (though perhaps generalized to work with other threaded
>SAPIs) to actually use some of those extra threads to link multiple real
>interpreters together (allowing one request chain to start/shutdown another
>in a sibling relationship -- as opposed to Runkit_Sandbox which is purely
>parent/child).  Don't look for this anytime soon though.  It's in my
>"wouldn't that be kinda cool" bin of projects to work on next to oggtheora
>and a few unnamed beasts...
>
>-Sara
>  
>
"kinda cool" to hear somebody is experimenting with this... :)...

---

## #21094 — Wez Furlong — 2005-12-08T02:24:59+00:00

URL: https://externals.io/message/21086#21094 · In reply to: #21091

Try `proc_open()`.

--Wez.


> Ilia Alshanetsky wrote:
> > […]
>
> That sounds sufficient enough. Although I guess this only works on *nix
> systems? Platform independence is one of the main reasons I prefer PHP
> over other languages.

---

## #21097 — Joseph Crawford — 2005-12-08T02:30:43+00:00

URL: https://externals.io/message/21086#21097 · In reply to: #21094

php should defenatly in the future support multi-threading especially if
they want to prove you can do desktop apps with php and not just web apps ;)

--
Joseph Crawford Jr.
Zend Certified Engineer
Codebowl Solutions, Inc.
1-802-671-2021
codebowl@gmail.com

---

## #21098 — Rasmus Lerdorf — 2005-12-08T02:42:05+00:00

URL: https://externals.io/message/21086#21098 · In reply to: #21097

Joseph Crawford wrote:
> php should defenatly in the future support multi-threading especially if
> they want to prove you can do desktop apps with php and not just web apps ;)

Which is not something we have ever stated to be a goal.

-Rasmus

---

## #21099 — Ilia Alshanetsky — 2005-12-08T02:48:59+00:00

URL: https://externals.io/message/21086#21099 · In reply to: #21091

Bart de Boer wrote:
> Ilia Alshanetsky wrote:
> 
> > […]
> 
> 
> That sounds sufficient enough. Although I guess this only works on *nix
> systems? Platform independence is one of the main reasons I prefer PHP
> over other languages.

fork() works on windows as well, but on Win32 forking is MUCH slower
then threads.

Ilia

---

## #21100 — Andrei Zmievski — 2005-12-08T03:08:48+00:00

URL: https://externals.io/message/21086#21100 · In reply to: #21087

Exactly. Something based on libevent would be nice.

-Andrei



>
>
> There are no plans for this.  Asynchronous mechanisms are generally  
> a much more efficient way to do this stuff.
>
> -Rasmus

---

## #21103 — Wez Furlong — 2005-12-08T03:17:51+00:00

URL: https://externals.io/message/21086#21103 · In reply to: #21100

pecl/event is available for download... ;-)

--Wez.


> Exactly. Something based on libevent would be nice.
>
> -Andrei
>
>
> 
> > […]

---

## #21106 — Lester Caine — 2005-12-08T06:38:13+00:00

URL: https://externals.io/message/21086#21106 · In reply to: #21098

Rasmus Lerdorf wrote:

> > […]
> 
> Which is not something we have ever stated to be a goal.

May be not, but when has that stopped people doing things ;)
Even when you TELL them something does not work, they will try anyway :(

-- 
Lester Caine
-----------------------------
L.S.Caine Electronic Services
Treasurer - Firebird Foundation Inc.

---

## #21108 — Bart de Boer — 2005-12-08T08:30:04+00:00

URL: https://externals.io/message/21086#21108 · In reply to: #21099

> fork() works on windows as well, but on Win32 forking is MUCH slower
> then threads.
> 
> Ilia

With fork() you mean using `exec()` to call php with another script, 
right? Because I can't seem to find the fork() function anywhere. :|

---

## #21110 — Bart de Boer — 2005-12-08T09:09:08+00:00

URL: https://externals.io/message/21086#21110 · In reply to: #21103

Thank you all, for the feedback. Some solutions are new to me. So, I'll 
be taking a deeper look into those options.

I have to say that they all still sound a bit like work-arounds. 
(Although this could very well be due to my unworthy knowledge. ;) ) I 
still think it would be nice if there would be something natively in PHP 
to which you could say: "This is for doing multithreading in PHP". It 
wouldn't nessacerily have to work exaclty the same way as it does with 
other languages. Maybe something like PCNTL that works cross-platform. 
Or something like what Sara is working on. Or maybe a special fork() 
function that minimizes overhead and has some PHP-optimized feedback 
features?

Loads of respect to you all!,

Bart

---

## #21111 — Lukas Smith — 2005-12-08T09:54:10+00:00

URL: https://externals.io/message/21086#21111 · In reply to: #21110

Hi,

I really do not see the point is worrying all too much about multi 
threading in PHP. Writing proper multi threaded code is hard and why 
bother? Your server is going to be busy doing tons of other things at 
the same time. So all your CPU's and cores should have plenty of things 
to do. Also PHP is a glue language, so by this virtue it should not 
require multi threading in order to get decent performance/latency for a 
request. Most of the work is performed elsewhere anyways (like inside 
your database server).

Of course you can use PHP to write deamons, hardcore number crunching, 
desktop apps. But this should never dominant decisions about the core of 
the language. So I welcome whatever Sara is playing with that can be 
added as an extension to PHP.

regards,
Lukas

---

## #21113 — Bart de Boer — 2005-12-08T11:17:36+00:00

URL: https://externals.io/message/21086#21113 · In reply to: #21103

> I really do not see the point is worrying all too much about multi
> threading in PHP. Writing proper multi threaded code is hard and why
> bother? Your server is going to be busy doing tons of other things at
> the same time. So all your CPU's and cores should have plenty of things
> to do. Also PHP is a glue language, so by this virtue it should not
> require multi threading in order to get decent performance/latency for a
>

History tells us that, as hardware performance increases, people will
always utalise the extra power by creating more demanding scripts (and
software). We most likely wouldn't do the things we do today if we still
had the hardware we had back when PHP3 was out. Since it currently seems
to be the trend that performance gains are made by adding more cores to
the processor, I think the server software must adjust to be able to
utalise this. I have a feeling we'll be seeing a lot more multithreading
software in the near future.

Although you're right that the webserver already does multithreading
itself. And PHP is able to hitch a ride on this capability. I just think
that maybe, if the multithreading trend really takes off, it may one day
become a necessity for PHP to support this. If so, I don't think it will
be in the near future. So there's still plenty of time to think about
this. :)

Of course this is all "what-if" speculation. But I thought it's worth
noting at least. :)



> the language. So I welcome whatever Sara is playing with that can be
> added as an extension to PHP.

---

## #21114 — Andrei Zmievski — 2005-12-08T17:21:15+00:00

URL: https://externals.io/message/21086#21114 · In reply to: #21103

It must be Christmas time.

- A



> pecl/event is available for download... ;-)
>
> --Wez.
