php 5 and mUltithreading

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ptobra
Forum Newbie
Posts: 11
Joined: Mon Jul 24, 2006 11:43 pm

php 5 and mUltithreading

Post by ptobra »

Does PHP5 supports Multithreading.???
if it is are there any docs regarding that?

Does PHP4 supports Exception Handling??
:?
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

PHP4 and 5 can fork itself pcntl_fork

PHP4 does not support exceptions.
ptobra
Forum Newbie
Posts: 11
Joined: Mon Jul 24, 2006 11:43 pm

wat is php_threads.dll

Post by ptobra »

I downloaded pear extension for php5. It contains php_threads.dll. how is it useful in running php code efficiently.
Any thoughts.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

As i know PHP supports multi-processing not multithreading.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yep, PHP does not support true multi-threading. The nature of the language just goes against it.

As has been pointed out, you can use pcntl_fork() to spur new processes.
ptobra
Forum Newbie
Posts: 11
Joined: Mon Jul 24, 2006 11:43 pm

Performance Issues

Post by ptobra »

What about the performance issues if we are creating processes using pcntl_fork();

Any other thoughts and example to support the processes control functions in php.
Can we use declare() and ticks to mimick multithreading. How??

:roll:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Performance Issues

Post by Chris Corbyn »

ptobra wrote:What about the performance issues if we are creating processes using pcntl_fork();

Any other thoughts and example to support the processes control functions in php.
Can we use declare() and ticks to mimick multithreading. How??

:roll:
Yeah, you will get performance losses from using pcntl_fork() since it's forking more processes. PHP wasn't made for this sort of programming really.... in fact it was enevr meant to be anything like as powerful as it is now... some people just liked it too much :)

Registering tick functions won't help much neither since all you'd be doing is running extra code every X ticks but it's still done in a linear fashion.

What do you need this for? Is there a reason it needs to be done in PHP?
ptobra
Forum Newbie
Posts: 11
Joined: Mon Jul 24, 2006 11:43 pm

Synchronization in pHp

Post by ptobra »

For multithreading i have integrated php and java and calling java Thread classes but still unable to achieve that.

Is there any way to achieve synchronization of variables or functions or code block as we have in java.

Thanks in advance :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Multithreading in PHP, or any Web app for that matter, for me atleast is not something I would want to achieve (especially forking)

If you create 2 threads for a page, you've essentially doubled the CPU workload, or at the very best, dramatically increased it.

That will happen for every page request. One of the sites I helped develop has 20,000 hits per month, that's not individual page requests for clarity, but 20,000 visits.

CPU and memory loads are already maticulously controlled.. so if I were to introduce multithreading and/or forking into the mix, it could well be a big bag of disasters.

The reason Java can implement threading so well, imo, is because it is more of a client run environment. The user's machine does a lot of the work with the Java Runtime.. In PHP/ASP/etc. it's the server that does it all.

However, a lot of this all depends in the implementation of threading within PHP and at the moment, I cannot see that being efficient, because PHP does not do multi-threading at the core level.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jenk wrote:The reason Java can implement threading so well, imo, is because it is more of a client run environment. The user's machine does a lot of the work with the Java Runtime.. In PHP/ASP/etc. it's the server that does it all.
That doesn't make any sense. A server is a computer.

...

PHP works quite literally like reading a script. It's just read linearly from top to bottom once it's been parsed. That's it.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

d11wtq wrote:
Jenk wrote:The reason Java can implement threading so well, imo, is because it is more of a client run environment. The user's machine does a lot of the work with the Java Runtime.. In PHP/ASP/etc. it's the server that does it all.
That doesn't make any sense. A server is a computer.

...

PHP works quite literally like reading a script. It's just read linearly from top to bottom once it's been parsed. That's it.
Often with Java developed applications, the load is shared between client and server, from the apps I have seen, a substantial amount of the 'load' per-connection is done by the client and not the server. Granted the server will perform more because of multiple connections.

Like you say, PHP is an interpreter - every request equates to reading a script, and to the best of my knowledge, a new thread create for every concurrent request. Be it an apache process/thread or a php process/thread.

So using multiple threads within an already multi-thread environment will drastically incur heavy load balance issues.

I could of course be completely wrong, or simply having the usual problem of not being able to explain my thoughts, but I am happy to be corrected :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Websites don't need threading. Period.

If you're really worried about it, register PHP as an Apache module and let Apache handle the threading issues.
ptobra
Forum Newbie
Posts: 11
Joined: Mon Jul 24, 2006 11:43 pm

How to achieve Synchronization of Variables.

Post by ptobra »

Can we achieve Synchronization of variables or code blocks in PHP???
:O
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to achieve Synchronization of Variables.

Post by Chris Corbyn »

ptobra wrote:Can we achieve Synchronization of variables or code blocks in PHP???
:O
No. It doesn't matter how you try to explain it. PHP can only do ONE thing at a time. Period.
Post Reply