Page 1 of 1

php 5 and mUltithreading

Posted: Tue Jul 25, 2006 4:55 am
by ptobra
Does PHP5 supports Multithreading.???
if it is are there any docs regarding that?

Does PHP4 supports Exception Handling??
:?

Posted: Tue Jul 25, 2006 6:49 am
by jamiel
PHP4 and 5 can fork itself pcntl_fork

PHP4 does not support exceptions.

wat is php_threads.dll

Posted: Tue Jul 25, 2006 7:32 am
by ptobra
I downloaded pear extension for php5. It contains php_threads.dll. how is it useful in running php code efficiently.
Any thoughts.

Posted: Tue Jul 25, 2006 9:51 am
by fastfingertips
As i know PHP supports multi-processing not multithreading.

Posted: Tue Jul 25, 2006 11:27 am
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.

Performance Issues

Posted: Wed Jul 26, 2006 6:58 am
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:

Re: Performance Issues

Posted: Wed Jul 26, 2006 7:25 am
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?

Synchronization in pHp

Posted: Wed Jul 26, 2006 8:36 am
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 :?

Posted: Wed Jul 26, 2006 9:06 am
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.

Posted: Wed Jul 26, 2006 9:18 am
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.

Posted: Wed Jul 26, 2006 10:04 am
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 :)

Posted: Wed Jul 26, 2006 5:43 pm
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.

How to achieve Synchronization of Variables.

Posted: Thu Jul 27, 2006 12:13 am
by ptobra
Can we achieve Synchronization of variables or code blocks in PHP???
:O

Re: How to achieve Synchronization of Variables.

Posted: Thu Jul 27, 2006 3:16 am
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.