- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
ScriptAlias / /www/cgi-bin/launch_nucular_missiles.php
Admin
You'd have to be doing something computationally mental on a server with a LOT of processor cores to make up for all the overhead you're introducing with this ridiculous method of "multithreading". And if you require communication between the "threads", it gets even better. Of course, the monkeys on your average PHP forum will probably recommend using the file system or database for IPC.
"I need to ask my friend for advice on whether to hammer the nail with a glass bottle or old shoe. Should I use cans-on-a-string or a carrier pigeon to call him?"
Admin
How do you think BCC works under the hood?
Hint: The SMTP server doesn't care what's in the To, Cc, Bcc header, it directs mail based on the RCPT TO command. Which, to my knowledge, only accepts a single address at a time.
Admin
Ever heard of apache-modules? No?
Admin
1)Some of the comments here are bigger WTFs than the original article. But, to be fair, I've seen code written in other languages that failed to multi-thread in an equally dismal manner. The difference is that it was written by people with many of years of experience.
A:You know, since PHP probably doesn't have a way to do multithreading B:Actually it does: http://ca.php.net/manual/en/function.pcntl-fork.php
A:Beautiful multi-threading. If I would have known a loop creates threads by itself it would have saved me so much time! Wow this was enjoyable B:It's not the loop that creates threads by itself. The loop writes img tags the the HTML. The browser will request each image from the server. These requests are executed in different threads on the web server so it really kind of does work. The amount of threads that will be parallelly executed depends on how many simultaneous requests a given web browser will make.
Reading these 3 comments back to back is truly a WTF! :P
Admin
(Some of the comments here are simply scary). The problem here is that of task division; there are two ways to do this right: the cheap way is a close analogue to what is presented here (but will not be interrupted if the client loses his connection, presses stop, surfs with images off, or just surfs away) by an fopen of an uninterruptible script handling the sub-task; the better way is to dump the receiver list in a database, start cron on the sub-task, have it generate mail for every record it can prune off the list, and have it turn off cron when done.
The solution is dangerous, but it has nothing to do with HTTP 1.1 suggested limits or raw stupidity; it just involves an unreliable browser in the critical role of marshaling the central core of the process. The real solution, though, looks quite a lot like what is being proposed here.
(Captcha: Burned)
Admin
Clever indeed. Stupid and not actually multi-threaded though, it's more like multi-processed.
Admin
Admin
Someone said this looked clever. I'll admit that I've had similar requirements before (but not exactly like this)
For example, image size generation on the server takes a while, but I need thumbnail and display sizes immediatly, but icons and details can take a while to generate.
The solution I take to do this is to have php shell out to another script, or application or whatever I want. If I want it to block the script then I run it in the foreground and wait for response.
If I want it in the background and not have to leave the user waiting, I run the shell scripts as a background shell process, essentially multitasking (image generation is now happening in the background).
Anyways, my solution might not be perfect, but at least it doesnt require client side code to initiate the threads, as everything is handled on the server. Although it still kind of has the parent.php child.php mentality to it.
Admin
RTFM is not about picking on anyone. It means what it means.
That someone has just answered in a funny and at the same time more or less correct way, trying to implant the clue.
Admin
I once maid a web application where you could click on a link and send out a report. Later I was asked if this could be done automatically each morning... so what did I do?
I set up a crontab with a wget call to the web page :-)
Later I learned that PHP had a CLI mode, so I switched of course.
Don't get me wrong. I don't normally use PHP for command line tools, but sometimes a web application needs to be updated automatically (such as generating reports), and why mix languages when PHP can do the work fine?
I also use Perl, Python and a bit of Java of course, depending on the task.
Admin
s/\bmaid\b/made/g and other corrections...
Please ignore spelling errors :-)
Admin
for($i=1;$i<=10;$i++){ echo "[image]"; } ?>
Okay... this is just wrong.. basically even if it DOES happen to call it ten times, it is calling the same start to finish through the get vars.. so you are emailing the same 1000 clients ten times over.
Admin
Yes, but once you send the mail to the server, PHP doesn't have to care what happens. The SMTP server probably uses some sort of threading to send mails anyway.
Admin
That is not parallel processing. A parallel algorithm should have at least two distinct tasks that later join to form a final result. Computers can do this using either threads or processes.
So, this person's PHP explanation was wrong on a few counts.
Admin
(Yes, I know, not all mail servers let you distribute to 1000 recipients at a time. You get the idea.)
Of course BCC isn't magic, the addresses still have to be issued individually as per the protocol, but don't underestimate the overhead of actually creating and destroying all the connections (not to mention the repeated HELOs and so on).
Admin
Yes, there are. See the Charles Babbage quote here.
Admin
I think the same person answered my own question about general multithreading at Slashdot recently. I simply want a good resource that will help me learn how to apply multithreading techniques in a shared memory space. What's the first reply? "Here's how you do it in PHP!"
I wanted to hit something.
Admin
The real WTF Is that nobody called him out on that board.
Admin
I'm not so sure what exactly would be wrong with having a multithreaded PHP script. Sure you wouldln't want to send anything to the response stream, but as long as you were careful, and gathered the data correctly at the end, you may be able to speed up execution. Think about it this way. Somebody does a search on your website. So, you start the search thread, that goes and gets the search results from the database. In the mean time, you load all the templates for displaying the data, and once the search is complete, you just fill in the templates. So you don't have to wait for the search to complete to load all your templates. Simplistic example. I'm sure some better examples could be found.
Admin
Thank you! This has to be one of the most entertaining WTFs I've read in a long time! The approach is brilliant! Here's someone who's taken an impossible problem, and solved it in their own, urm... unique way. I love it!
Admin
Admin
Another example would be a PHP script connecting to a web service. Then the script could have multiple connections if it needs to. I have often seen web services that are too fragmented, where you have to combine 2 or 3 sources to get the information you need. If this can be done at the same time, you should use threads
Admin
Not being able to use multiple threads is a wonderful, sensible language limit.
My mental picture of Java is a tricycle -- not too quick, but hard to hurt yourself with -- with a chainsaw called threads bolted to the front.
I'm imagining with ColdFusion (I haven't had the pleasure of using this), they've added an extra wheel or two for extra stability. One hopes they also took off the chainsaw.
People who actually understand threads are rare and strange and twitch a lot. They tend to wake in a cold sweat in the middle of the night and start raving about race conditions.
Let your database handle concurrency.
Admin
Thank you, that's definitely going into my quote collection :)
Admin
firefox with the fasterfox extension and specific settings might indeed open many connections at once, but i'm not too sure about this - how does it combine with http keep-alive and/or pipelining which would negate this and again force the requests to wait for each other and reuse the connection?
and even if he does get many connections, lots of default php configurations use auto-started file-based sessions with locks so a second script from the same session will hang around and wait for the earlier scripts to finish...
Admin
Java vs C++ benchmarks: http://www.freewebs.com/godaves/javabench_revisited/
So assuming Java is 2-5 times slower than C++ on low level tasks, I'd rather compare them as Java == Volvo vs C++ == Hotrod. Java/Volvo for the lower speed and for safety design protecting the users when things go wrong. C++/Hotrod for optimum speed but intended to be used only in designated places and by experts, who are aware of the risks.
(Sorry for any bad grammar. English is not my first language.)
Admin
This being later than 1985, that's the only reason you can get away with using the phrase "hot rod" in a serious discussion.
Admin
...which is generally only two.
Admin
Intended to write Ferrari first, but the problem is that a Ferrari, unlike c++, have quite a lot of safety build in today. Hot rod was what i came up with as a very fast machine that is quite unsafe to crash with =)
Admin
Next week he'll discover that he needs to share some data between the threads, and the week after that he'll discover this great thing called "race conditions", and find that there's no language mechanism to deal with them.
Though I fully expect someone will now come up with a synchronization primitive for PHP in this thread.....challenge starting in 3..2..1..
Admin
If you read the post I was replying to, you'd see that it was claiming that sending multiple emails would get you spam blacklisted (presumably by your local SMTP server, since [ESPECIALLY if they're sent as individual emails] the recipient systems don't know how many people you're sending it to) somehow, when in reality you're sending the same number of emails whether you put all the addresses in To:, Cc:, Bcc:, or send them individually with only one address each in the To: header.
Addendum (2007-08-14 09:07):
Or it requires running /sbin/sendmail 1000 times to submit the messages into the local queue. And while the post doesn't go into detail on exactly how the emails are sent, we can speculate that he most likely uses the built-in mail() function, which does exactly that on unix systems.Admin
Not really a 'naive' question, unless he said 'mod_php' instead of 'php'. It's perfectly valid to use PHP in commandline apps, in which case, the following package might solve the problem:
http://pecl.php.net/package/threads
Admin
As an aside, the thread/fork discussion did pick up within hours after that WTF post, and continued a bit afterward. Of course, the guy that posted that WTF never bothered to show himself around our community after that.
CAPCHA: burned (seems almost appropriate, doesn't it?)
Admin
You shouldn't be using it, then. WHY WOULD YOU EVEN DO THAT, REINVENTING THE WHEEL? PHP is already exactly Perl, only worse.
And when you're done with that, I need you to take dictation in triplicate for me on lined and unlined paper.
Admin
PHP for system management scripts? Are you mad? The only excuse for ever using PHP from the commandline outside of a web app is if you absolutely require a certain library that isn't available for other languages. For example, I once had to write a commandline script in PHP because I required the FX.php library (has to do with Filemaker Pro). If I could have done it with Perl or Ruby or Python or a shell script, I would have in a heartbeat. PHP is such a braindead, ugly language that is specifically designed for web pages. Use the right tool for the job, for most things outside of dynamic web pages, PHP is not it.
Admin
hahahaha! funniest thing I've read in ages, coldfusion!?!!!
Admin
forking new processes is not multi-threading, it's multi-processing
Admin
However avoiding this is what simplistic-servers-with-reverse-proxies like nginx were created for. Or, for really heavy load, yaws.
Admin
everyone has missed the true wtf.. the submitter must have been searching for "Multi threaded PHP" on Google to find a 4 year old forum thread about it.
captcha: dreadlocks
Admin
http://www.caucho.com/resin-3.0/quercus/index.xtp
this is real guys. the php code is straight compiled to java-bytecode. I tried threads with it, works like hell.
Admin
x2 ;]
I've never quite worked out why anyone uses CF. Hosting for it is $$$$ and there's not exactly a huge community making nice open sourced software, frameworks, etc for it.
Admin
To be frank, and I don't mean to be inflammatory, just practical and fact-oriented, PHP's only positive is its wide deployment. The libraries and their ekhm, consistent, ekhm, set of aptly named functionality is something to be despised, and undoubtedly a source of a s***load of errors, er, I mean fun in the industry. The backend, including Zend, seems perfectly good, but the libraries are just plain unprofessional. Heck, they are unprofessional when compared to say Win32 API, which in itself is not something to be overly proud of.
I don't know Perl's libraries (at least those considered to be core-well developed ones), so I can't comment on that. Assuming they are OK, Perl as a language would be a decent-enough replacement for PHP for my use -- the core language features are a reasonable mix, and a good enough approximation of Lisp ;) What I do know is that, having coded a few small web applications in PHP, I won't ever do it again if I have the control over where it's gonna be deployed.
For most part, whatever web-based I have to do these days is done in Lisp, and I don't really see a reason to use PHP given that any Unix-y Lisp worth its salt can easily call native C library functions. I wish Allegro CL-based application servers were as widely deployed as PHP, it'd make the damn thing cheaper. As it is, I don't use it, for low-volume work SBCL is good enough for me.
Heck, I write embedded software for tiny microcontrollers (as in 4kB RAM tiny) in a Lisp dialect. At least then I don't need hacks like assembly-time macros written in assembly. Yuck. And they teach with that :( <rant>It takes, IMHO, a special sort of insight, to advocate writing domain-specific embedded languages (assembly-time, not run-time) not only in assembly, but actually developing the high-level assembler (HLA) to do just that. That's like doing everything in one's power to avoid Lisp. Oh well...</rant>
Saying that "PHP is fine for small jobs" is essentially suggesting that peoeple learn something that won't ever be useful if they intend to grow.
As to the WTF at hand, I see a couple things. First of all, for such an application, once the request gets to the web server there should be no involvement of the requestor (the browser), other than maybe to provide feedback to the user on the progress. Secondly, given that it was asked on the PHP forum we're stuck with PHP, there are proper, if hacky, was of sending as many emails as you wish even in execution-time-limited environments.
Probably the simplest way to deal with time limits is to emulate continuations in PHP, and have the script exec another one to continue its work, once it has been sending for say 90% of the allotted time. The chain of exec-ing scripts would be fired off from the main script by fork+exec. This would likely be the least WTFy way given the assumed circumstance of being runtime limited - we have to deal with two problems: finishing the original request in reasonable time, and keeping the PHP instances from running out of time.
A separate script would handle the AJAX or simple refresh side of things for the feedback. The feedback and worker scripts can communicate via some sockety mechanism, or, if allowed, can simply keep the feeback appended to a session-specific file which is then read by the feedback script. Given WTFy nature of PHP itself, that's all there is to it.
PHP feels like working in bash with more library functions. The available level of expression is about the same.
Cheers!
Admin
Kuba, I'm the first one to agree that PHP has all sorts of quirks, inconsistencies and odd limitations. People say it's the bastard child of Perl, but I say it feels more like the bastard child of C and Bash. But still, many of the things people DO with software are simple. Writing a script to open a database once a day, run a query, and based on the query save certain files here or there... not so complicated. Ergo if I were advising a PHP developer who just needed such a thing, I would say just do it in PHP. What's the big deal?
But honestly, many of the WTFs are informational in nature... viz. the inconsistent function naming, or the fact that even fairly knowledgeable people like yourself don't seem to realize that the script time limit can be turned off. In fact, spending a serious amount of time researching all the "I hate PHP" web articles out there, I found that at least 90% of the WTFs were gone somewhere between PHP 4.3 and 5.2.
Yes, PHP has no elegance. I much prefer the more functional-oriented kinds of languages like Python or Ruby for general scripting, but in spite of all the whining I've seen out there, I've managed to be actually accomplish something in PHP if that's the requirement. Generally it's best to approach PHP as if C had suddenly magically been turned into a scripting language. It's even got something
BTW, I have to laugh... even IBM is on this game: Develop multitasking applications with PHP V5
Like it or not, PHP will be gradually coerced into becoming a general language. Sigh... now if they would just add first-class functions, closures, namespaces... ahh never mind! We know that's not in the cards.
Admin
if you need to thread, why are you using php?
Admin
... er, I've forgotten my point here.
Oh yes.
Why would any sane human being want to make PHP scalable? It just isn't. It was never designed to be. It's mighty fine in its own way, but that way is the way it should be left in. If you're expecting the average PHP application to cope with more than fifty or so simultaneous connections, then you've made a bad and business-threatening mistake.
And even if you do want to make PHP scalable, this is hardly a sensible way to do it.
Admin
Admin
holey fork...
what else can I say?
Admin
About pcntl_fork(): The process creation functions of PHP must not be used in a web server environment, so in this case it's useless.
About multithreading in PHP: The web server manages the threads. Apache 1.3 with mod_php was single-threaded. So you can create as many requests as you want, only one will be executed at a time. I don't know about Apache 2 with mod_php, but any web server using FastCGI runs multiple instances of PHP at the same time, so parallelism is possible in this case. Of course this is not what I call "multithreading"...
Admin
My webhost allowed me to send > 20 000 huge mails to a GMail-address (mine), and it hasn't been blacklisted anywhere yet. Oh, and yes, GMail really does have all the storage it says it has. :)