473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
May 15 '06 #1
5 1823
aladdin wrote:
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?


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
May 15 '06 #2
aladdin wrote:
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?


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.
js*******@attglobal.net
==================
May 15 '06 #3
In article <NO******************************@comcast.com>,
js*******@attglobal.net says...

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.


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
May 15 '06 #4
to**@tony.com wrote:
In article <NO******************************@comcast.com>,
js*******@attglobal.net says...

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.

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


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.
js*******@attglobal.net
==================
May 15 '06 #5
to**@tony.com wrote:
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


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.

May 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
7
by: tah | last post by:
Hey, Can someone please clarify, confirm, or set me straight on my understanding of a standalone="yes" attribute in the xml version element? I assume it means that the xml document containing it...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.