• streaky (unregistered)

    Yeah, this is forking not threading.

  • streaky (unregistered)

    In fact, just looking at the code again, this is pretty absurd, shouldn't tell people to do stuff like that because it's.. bad in every way.

    There are proper ways of forking process y'know? Maybe they aren't beautiful but they're better than this.

  • Reed (unregistered)

    This is actually kind of an interesting hack, in my opinion. Each image could actually be a segment in a progress graphic.

    Only problem is that it's completely unreliable. And you have no way of knowing when (or if) it ever completes.

  • eggs (unregistered)

    My god, I nearly threw up.

    The sad part is, I did this once, but it was just a quick hack when I wanted to call a script on someone else's page 5000 times. Not production code, but rather "I want to do something just for fun so I'll just hack up a script in 5 minutes and go play Nintendo while the computer chews on it".

    Using this in production code would be a real WTF.

  • Mythras (unregistered)

    There is actually a way to support threads using PHP! It's been available since PHP 4.07, and it's basically the same way you do it in C, using a "fork" function. Here's a snippet that shows how to do this:

    <?php $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid) { // we are the parent; do parent stuff pcntl_wait($status); //Protect against Zombie children } else { // we are the child; do child stuff } ?>

    What happens is when we call the pcntl_fork() function, a child process that differs from the parent process only in its PID and PPID is created (ie, they share the same code). When the parent process issues the pcntl_fork() call, $pid holds the child's process id. When the child issues the pcntl_fork() call, $pid will hold 0. This way, we can distinguish who the parent is and who the child is and make them do different things.

  • Helmut (unregistered) in reply to Lingerance

    Mulitthreading is something different from multiprocessing

  • Rick (unregistered)

    Wow, am I really the only one that noticed 'weight' in the code instead of 'height'??

  • Don't worry about silly language limits lmao (unregistered) in reply to Russ
    Russ:
    (I don't have to worry about silly language limits like this since I code in ColdFusion and can always write a piece in pure Java if necessary, as CF runs on top of Java), but this seems a pretty brilliant way to do multithreading (although he probably should've used better practices to avoid sql injection, etc).

    Yeah brilliant way "to do" multi-threading... Even though the only resemblance this would have with multi-threading is that more than one process is being run the same time as the other.

    But then again you don't have to think about this since you're using your no-limit Markup Language CF.

    gw FUD-monger stick to your unlimited language - ColdFusion cues "no-limit" music

  • Don't worry about silly language limits lmao (unregistered) in reply to Rick
    Rick:
    Wow, am I really the only one that noticed 'weight' in the code instead of 'height'??

    yes. that means you're special. go knock the guy down some more. I'm sure you've never misspelt a word...

  • Kuba (unregistered) in reply to rycamor
    rycamor:
    or the fact that even fairly knowledgeable people like yourself don't seem to realize that the script time limit can be turned off.

    I do realize that. The presumption is that the server is set up in a certain way you have no control over. Think shared hosting.

    Cheers!

  • (cs)

    The REAL, REAL WTF is image caching :)

  • anon (unregistered)

    http://dev.pedemont.com/sonic/

    threads forks, same shite on the outside

  • Chris McIntosh (unregistered) in reply to Martin
    Martin:
    The problem is that this guys thinks he know the buzzword to the solution of what he wants to do - which is not threading, but avoiding timeouts.

    It's pretty common for PHP scripts that take a long time to execute to bite the task at hand into smaller chunks that can be executed quickly, and thus avoiding asking your users to edit the "max_execution_time" (or whatever it's called) setting, which isn't available in many shared hosting setups.

    And the solution given isn't a WTF, it's just bad PHP quickly churned out on a forum.

    I think the better option is to do this in a oop fashion. Nothing wrong with using a web-browser to do the multi-threading if you dont know how it gets the task accomplished just as if you did something command line. As far as the time out issues goes, there is a method that will allow a php script to reset the timeout on a server. I have a web crawler that does just this and will run indefinitely if I so choose to let it.

    My web crawler is probably one of the best examples to the need or use of mutli threading and the time out issue. It also runs with a mysql-db back-end for recording various results, its work log, and its work que(which is different from the log). I make use of classes, objects, and the even some defines in my code(mostly for readability and re-usability).

    Im starting a long series of tutorials on it on my site if it helps anyone Chris McIntosh Designs just take a look under the computer tutorial section.

    As far as cold fusion or any language being better than another I believe that is all hog wash. it is not the language as so much as it is the programmer's familiarity with the language he is using. I have done some amazing things with just actionscript that would be a rival to things you can do in php, asp, or cf all of which I have programming expertise in. Anyway, keep the bias for languages down and think more as a engineer and use lots of placebo-code to do your initial programming. Break it down to its smallest components and build up from there.

  • Ujang (unregistered)

    to its parent? WTF

  • Montag (unregistered)

    Actually wrote something like that back in the 90's using ASP. It worked well at preventing script timeouts for request initiated processess that were likely to take a long time on a shared web server.

  • razzamatazz (unregistered)

    thank you ever so much for NOT ANSWERING THE QUESTION! that example shown, has nothing to do with multi-threading.. the emails are still sent synchronously.. what a waste of web space!

  • Anton (unregistered) in reply to razzamatazz

    have a look for Multiplatform PHP multithreading engine http://anton.vedeshin.com/articles/lightweight-and-multiplatform-php-multithreading-engine

  • rolfen (unregistered)

    LOL I think that stuff can be done with PHP3...

  • rolfen (unregistered) in reply to Reed

    Well if its you who are calling that page in your browser, then you can call the mailing script through javascript (from javascript code, AJAX or whatnot) In this case there might be a way to see progress, and error messages. The mailing script can also be made to log results somewhere.

    Thats not too bad, and has advantages. It does look better from the web hosts perspective then having a php script fork a dozen of children, and is actually easier to code.

    Its just that I would never have thought of it hadnt I read it here...

  • Buhrietoe (unregistered)

    Those answers are wrong. You can do true multi-threading in PHP. The only limitation is it has to be run in CGI form, not an apache module. Just use the PCNTL functions. http://php.net/pcntl_fork

  • EXiUS.net (unregistered) in reply to Buhrietoe
    Buhrietoe:
    Those answers are wrong. You can do true multi-threading in PHP. The only limitation is it has to be run in CGI form, not an apache module. Just use the PCNTL functions. http://php.net/pcntl_fork

    useful if you have 100% control over your own web server. don't expect much from shared hosting...

  • EXiUS.net (unregistered)

    Question:

    Is it possible to return/retrieve a value sent to an IMG tag?

    Example:

    <? // requester.php echo '<img src=\"http://exius.net/processor.php?id=5\">'; echo "new value: $returned; ?> <? // processor.php $returned = $id++; ?> <!--requester.php outputs-->

    new value: 10

  • EXiUS.net (unregistered)

    found something called cURL. it is not exactly the answer, but it can return values produced by an independent php page, whilst your initiator page will have to wait for a finish before loading the next function.

    not ideal for threading. takes me 3 seconds for a simple cURL request, while only 0.02 seconds or less for IMG sendage.

    sample: http://forums.digitalpoint.com/showthread.php?t=141762

  • Jonesy (unregistered) in reply to gygax
    gygax:
    Mr. Garrison:
    Despite what our teachers may have said, there is such a thing as a stupid question.
    There are no stupid questions, only stupid people!
    Only stupid people believe that quote. There really are stupid questions.

    How do u post comments on TDWTF??

    ^^^stupid question

  • mikeytown2 (unregistered)

    This can be done using HTTPRL or Guzzle. http://drupal.org/project/httprl/ https://github.com/guzzle/guzzle

Leave a comment on “Multi-threaded PHP ”

Log In or post as a guest

Replying to comment #:

« Return to Article