473,657 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1834
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*******@attgl obal.net
=============== ===
May 15 '06 #3
In article <NO************ *************** ***@comcast.com >,
js*******@attgl obal.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*******@attgl obal.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*******@attgl obal.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_bloc king() 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
3722
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
8493
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
2133
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 when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
9
2455
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 should build a database-connection and send a few sqls
2
2307
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 I just wanted to ask if I am missing anything based on the following scenario. My test app pulls data from a large external data source which has a table-like structure (but not rdbms - more
55
3299
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 call operations on Controls from a delegate, otherwise it does not work. However each time I've done an operation, I must update the progressbar and progresslabel, but this cannot be done in the delegate as it does not work.
5
2483
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 multithreading online using C# .
7
11655
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 is standalone, and does not refer to any external document to define types. In other words, it doesn't use an external dtd to validate any types - everything used would be defined within the doc itself. In other words, you would never see an xml...
7
16301
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 Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.