Re: [PHP] Multithreading for OOP PHP

2012-10-31 Thread ma...@behnke.biz
Florian Müller florian-muel...@outlook.com hat am 31. Oktober 2012 um 07:58
geschrieben:
 Hi guys
 I was wondering, what actually the reason is that PHP itself does not support
 multi-threading?
 I guess this would be realizable, or not? If not, why?
 Maybe this is a stupid question, but still somehow interesting. Realization in
 a way as Java does (or just something in that way) would actually be a very
 nice thing, don't you think?
 Thank you for your answers :)

That is because most parts of the PHP code is not threadsafe.

 Florian
 
--
Marco Behnke
Dipl. Informatiker (FH), SAE Audio Engineer Diploma
Zend Certified Engineer PHP 5.3

Tel.: 0174 / 9722336
e-Mail: ma...@behnke.biz

Softwaretechnik Behnke
Heinrich-Heine-Str. 7D
21218 Seevetal

http://www.behnke.biz

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Multithreading for OOP PHP

2012-10-31 Thread Florian Müller
I actually tought about just the same structures as Java uses(something in this 
way:
Thread t = new Thread(new Runnable() {public void run() {...blabla  
 }}
I thought this would actually be a good benefit if PHP supported this. It's 
just as we sometimes use PHP for doing some big Server works (e.g. database 
copying or something) and it would be nice to controll by yourself which Thread 
(or process) does which part of the job.
For normal HTTP calls which must be handled within milliseconds, this actually 
does not make sense, that's right ;)

 From: ovidiugabr...@gmail.com
 Date: Wed, 31 Oct 2012 10:59:28 +0200
 To: php-general@lists.php.net
 Subject: [PHP] Multithreading for OOP PHP
 
 Hello Florian,
 
 Usually you want to run a PHP script in two different environments:
 1. inside a webserver
 2. in a command line interface
 
 For a web application, the application server does the work for you.
 It is the server job to have a thread pool and balance it correctly.
 So you don't need threads for the same reason threads are prohibited in
 Java EE containers.
 I assume that you don't fork multiple threads for every single HTTP request
 inside a Java Servlet, for instance.
 
 When you are inside a command line usually you start processes instead of
 threads.
 And this is fully supported by PHP.
 However, there are several reasons in choosing processes instead of threads.
 Several high-level programming languages are starting OS processes for
 language threads.
 But maybe this is not on our topic now.
 
 But I am wondering what do you mean by multithreading support?
 Do you need library functions as synchronization primitives?
 Or you are talking about something like synchronized keyword?
 
 
 
 On Wed, Oct 31, 2012 at 8:58 AM, Florian Müller florian-muel...@outlook.com
  wrote:
 
  Hi guys
  I was wondering, what actually the reason is that PHP itself does not
  support multi-threading?
  I guess this would be realizable, or not? If not, why?
  Maybe this is a stupid question, but still somehow interesting.
  Realization in a way as Java does (or just something in that way) would
  actually be a very nice thing, don't you think?
  Thank you for your answers :)
  Florian
 
 
 
 
 
 -- 
 Cu respect / Best regards,
 Dipl.-Ing. Ovidiu Farauanu
  

Re: [PHP] Multithreading for OOP PHP

2012-10-31 Thread ma...@behnke.biz


Ovidiu Farauanu ovidiugabr...@gmail.com hat am 31. Oktober 2012 um 09:59
geschrieben:
 Hello Florian,

 Usually you want to run a PHP script in two different environments:
 1. inside a webserver
 2. in a command line interface

 For a web application, the application server does the work for you.
 It is the server job to have a thread pool and balance it correctly.
 So you don't need threads for the same reason threads are prohibited in
 Java EE containers.
 I assume that you don't fork multiple threads for every single HTTP request
 inside a Java Servlet, for instance.

 When you are inside a command line usually you start processes instead of
 threads.
 And this is fully supported by PHP.

The drawback of forking is the memory overhead. With every fork you take the
same amount of memory which is not the case if you could use real threads.

 However, there are several reasons in choosing processes instead of threads.
 Several high-level programming languages are starting OS processes for
 language threads.
 But maybe this is not on our topic now.

 But I am wondering what do you mean by multithreading support?
 Do you need library functions as synchronization primitives?
 Or you are talking about something like synchronized keyword?

Since PHP is not threadsafe, it is not possible to run the APACHE in worker
mode, you have to use the prefork model.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Multithreading for OOP PHP

2012-10-31 Thread ma...@behnke.biz


Ovidiu Farauanu ovidiugabr...@gmail.com hat am 31. Oktober 2012 um 09:59
geschrieben:
 Yes Marco has right.

 But more than that, OOP is mainly designed to run in a single threaded

. I don't put a comment on that.

 environment and it is not the best idea to be used for concurrent
 programming because you will need synchronization everywhere and this slows
 down the code, but also ask for a lot of other troubles.

Well, just avoid the difficulties of creating a good program, because it is too
much efford?


 I think functional paradigm is more better suited for concurrent
 programming, and there are some nice features in PHP 5.4
 please see Closures and how callbacks can be used in PHP. Functional
 features are prepared for Java 8, and they are already in C++11.

What have closures and callbacks to do with concurrent programming?
Nothing. The execution is serialized. There is no parallel in it.

Together with your last message on the list on the same topic, you should
consider reading a bit more on that topic.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Multithreading for OOP PHP

2012-10-31 Thread Larry Garfield

On 10/31/12 1:58 AM, Florian Müller wrote:

Hi guys
I was wondering, what actually the reason is that PHP itself does not support 
multi-threading?
I guess this would be realizable, or not? If not, why?
Maybe this is a stupid question, but still somehow interesting. Realization in 
a way as Java does (or just something in that way) would actually be a very 
nice thing, don't you think?
Thank you for your answers :)
Florian


PHP is by design a shared-nothing architecture.  What's happening in one 
process/request cannot impact what's in another request.  They may both 
write out to the same external service (DB, memcache, etc.), but they do 
not interact.  That makes the program much much simpler, since you don't 
need to worry about state synchronization.  It also means you can scale 
up by just adding more servers, because you don't need to deal with oh, 
now the memory isn't shared, so now what do I do?  Each PHP process 
could be in its own CPU core, CPU, server, or server cluster, and the 
code doesn't change in the slightest.


The shared nothing architecture is a very deliberate design decision, 
and is in a large part responsible for PHP's success.


--Larry Garfield

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Multithreading for OOP PHP

2012-10-31 Thread Matijn Woudt
On Wed, Oct 31, 2012 at 11:37 AM, Florian Müller
florian-muel...@outlook.com wrote:
 I actually tought about just the same structures as Java uses(something in 
 this way:
 Thread t = new Thread(new Runnable() {public void run() {
 ...blabla   }}
 I thought this would actually be a good benefit if PHP supported this. It's 
 just as we sometimes use PHP for doing some big Server works (e.g. database 
 copying or something) and it would be nice to controll by yourself which 
 Thread (or process) does which part of the job.
 For normal HTTP calls which must be handled within milliseconds, this 
 actually does not make sense, that's right ;)


Hi,

Have you seen the pcntl[1] and pthreads[2] extensions?

pcntl extension provides a pcntl_fork function, which is also some
kind of threading. Pthreads is almost like it's Java equivalent.

- Matijn


[1] http://www.php.net/manual/en/book.pcntl.php
[2] http://www.php.net/manual/en/book.pthreads.php

Ps. Please don't top post on this mailing list.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Multithreading for OOP PHP

2012-10-31 Thread Dotan Cohen
 It's just as we sometimes use PHP for doing some big Server works (e.g. 
 database copying or something) and
 it would be nice to controll by yourself which Thread (or process) does which 
 part of the job.


I don't suppose that the client is sitting there waiting for a reply
until the browser times out while the code is database copying or
something. That is why you should hand off the database copying to an
application that does that and then return to the client a response
quickly.
exec(mysqldump);

You could even give the client and AJAX page and inform them of the
status of the operation if the application that you passed off the
work to supports it.

I have wished for PHP threading many times, and my current big
client also asked for PHP threading recently. Yet, every single time
that I've devised an alternative solution I was glad that I did. PHP
is not memory efficient and we really don't need it hogging up the CPU
when a real C program can do the intensive work. And if you need
threading, then you are very likely looking at exactly the type of
workload that PHP is designed not to do!

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php