Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 15th, 2006, 02:45 AM
aladdin
Guest
 
Posts: n/a
Default Multithreading for standalone php

Hi, all.

I'm trying to write a mutlithreading server with standalone PHP.
Concurrent requests is possible. Normally it is done by handling each
socket spawn by serversocket in separate threads. But it seems that
there is no thread support in PHP at all.

Would anyone give me some suggestions?

BTW, is the community considering add thread support to PHP in the
future? What is the major difficulty to import an existing thread
library (such as pthread library) into PHP?

--
aladdin
  #2  
Old May 15th, 2006, 02:55 AM
Chris Hope
Guest
 
Posts: n/a
Default Re: Multithreading for standalone php

aladdin wrote:
[color=blue]
> Hi, all.
>
> I'm trying to write a mutlithreading server with standalone PHP.
> Concurrent requests is possible. Normally it is done by handling each
> socket spawn by serversocket in separate threads. But it seems that
> there is no thread support in PHP at all.
>
> Would anyone give me some suggestions?
>
> BTW, is the community considering add thread support to PHP in the
> future? What is the major difficulty to import an existing thread
> library (such as pthread library) into PHP?[/color]

You can fork a process but not do multi-threading.

More info here:
http://www.php.net/pcntl_fork
http://www.electrictoolbox.com/artic...ocess-forking/

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
  #3  
Old May 15th, 2006, 12:15 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: Multithreading for standalone php

aladdin wrote:[color=blue]
> Hi, all.
>
> I'm trying to write a mutlithreading server with standalone PHP.
> Concurrent requests is possible. Normally it is done by handling each
> socket spawn by serversocket in separate threads. But it seems that
> there is no thread support in PHP at all.
>
> Would anyone give me some suggestions?
>
> BTW, is the community considering add thread support to PHP in the
> future? What is the major difficulty to import an existing thread
> library (such as pthread library) into PHP?
>[/color]

Hmmm, as much as I like PHP, I might suggest it is not the right approach for
this operation. PHP doesn't support threads, but you can fork a new process (on
Unix) as Chris indicated.

Personally I'd look at another language for this, such as C or C++.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #4  
Old May 15th, 2006, 01:25 PM
tony@tony.com
Guest
 
Posts: n/a
Default Re: Multithreading for standalone php

In article <NOSdnYkKFc-0_PXZnZ2dnUVZ_vWdnZ2d@comcast.com>,
jstucklex@attglobal.net says...
[color=blue]
>
> Hmmm, as much as I like PHP, I might suggest it is not the right approach for
> this operation. PHP doesn't support threads, but you can fork a new process (on
> Unix) as Chris indicated.
>[/color]

This is interesting. I'm working on a project that will need to connect
to 2 different servers and to speed things up (being ssl) I was hoping to
be able to do this simultaneously.

I need my web visitor to hit a button and be able to enter information
on another server while at the same time my system is looking up data
in a database on a third server.

Would this be the sort of thing possible with PHP (my server uses apache2
with CGI) or would it have to use threads?

tony
  #5  
Old May 15th, 2006, 03:25 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: Multithreading for standalone php

tony@tony.com wrote:[color=blue]
> In article <NOSdnYkKFc-0_PXZnZ2dnUVZ_vWdnZ2d@comcast.com>,
> jstucklex@attglobal.net says...
>
>[color=green]
>>Hmmm, as much as I like PHP, I might suggest it is not the right approach for
>>this operation. PHP doesn't support threads, but you can fork a new process (on
>> Unix) as Chris indicated.
>>[/color]
>
>
> This is interesting. I'm working on a project that will need to connect
> to 2 different servers and to speed things up (being ssl) I was hoping to
> be able to do this simultaneously.
>
> I need my web visitor to hit a button and be able to enter information
> on another server while at the same time my system is looking up data
> in a database on a third server.
>
> Would this be the sort of thing possible with PHP (my server uses apache2
> with CGI) or would it have to use threads?
>
> tony[/color]

Hi, Tony,

Hmmm, now you're changing things a bit. You have a web visitor to work with it,
and using SSL in addition. This makes things a lot more complicated in C or C++
- especially the SSL end unless you're already familiar with the SSL
functions. And working with a web user is a lot different than just stand-alone
command line options.

You probably could still spawn a C/C++ program to do the database work (as long
as it isn't using SSL), but I'm not sure it's worth the extra effort and
complications.

Since you're working with remote databases, threads are probably going to
increase your throughput. But the real question is how long does it take to do
the work on the remote server? You're going to be constrained by the slowest
operation, of course. If both operations take 10 seconds, you'll save about 10
seconds by doing them concurrently. But if one takes 19 seconds and the other
only takes 1 second, your savings will be 1 second - despite the 20 second total
in both cases.

To do it, you'd have to spawn another process to do the work and wait for it to
respond. This part isn't hard. And even passing the necessary values isn't too
bad. Getting the returned data isn't too bad - there are several ways to do it
but you'll have to parse the results somehow.

The real problem comes in handling errors. For instance, what if one database
(or the path to it) is down? Or data which is supposed to be there isn't found?
Or any of dozens of other things which can (and do) go wrong?

If you're doing it all sequentially in PHP this becomes much easier to handle.
But when spawning the external process you've added another layer of complication.

Just some things to think about.




--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #6  
Old May 15th, 2006, 04:55 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: Multithreading for standalone php

tony@tony.com wrote:[color=blue]
> This is interesting. I'm working on a project that will need to connect
> to 2 different servers and to speed things up (being ssl) I was hoping to
> be able to do this simultaneously.
>
> I need my web visitor to hit a button and be able to enter information
> on another server while at the same time my system is looking up data
> in a database on a third server.
>
> Would this be the sort of thing possible with PHP (my server uses apache2
> with CGI) or would it have to use threads?
>
> tony[/color]

The easiest way to do multitasking in PHP is to let the web server
handle the processes/threads. Instead of doing the time consuming task
directly--in this case retrieving data from a remote server--use a
child script, which the parent script then request multiple times.

In the child script, the first thing you do is call flush() so that an
fopen() to it would return immediately (flushing sends the HTTP
header). After that, proceed with doing the time-consuming task,
echoing whatever the result is.

In the parent script, connect to instances of the child script via
fopen(), storing the handles in an array. Use stream_set_blocking() to
make the streams non-blocking. Then use stream_select() to check which
streams has data available to read.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.