PHP Multi-threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Maicovschi

    PHP Multi-threading

    As multi-threading is not built in PHP I've been using a hack letting
    the Apache server handle the multi-threading issues, but I'm really
    curious of other approaches to this issue.

    If anyone has any ideas I'd be more than glad to hear them up and
    discuss them.
  • Erwin Moller

    #2
    Re: PHP Multi-threading

    George Maicovschi schreef:
    As multi-threading is not built in PHP I've been using a hack letting
    the Apache server handle the multi-threading issues, but I'm really
    curious of other approaches to this issue.
    >
    If anyone has any ideas I'd be more than glad to hear them up and
    discuss them.
    Hi George,

    I take it you are working on Apache2?
    Well, as far as I understood PHP isn't threadsafe (yet).
    Not to mention all the additional packages (for DB access eg).

    I wouldn't go for such an approach in any productionenvir onment.

    The following articles might shed some light on the issues:


    If you took everything you heard for granted, you’d have been lead to believe that the official PHP distributions (from php.net) have been thread-safe since version 5.2.0. That’s not tr…


    Unless you have some compelling reason to use a threaded webserver, I
    think, for now, the best option is fastcgi, if performance is an issue.

    If you are just experimenting, consider this message not sent. ;-)

    Regards,
    Erwin Moller

    Comment

    • George Maicovschi

      #3
      Re: PHP Multi-threading

      Hey Erwin,

      It's just an experiment of mine, nothing production-related... YET :-)

      The thing goes like this. I have a script that's supposed to do an
      action on lets say 100 different hosts in the same time. So I need
      multi-threading ar a way to emulate it. My approach was this:

      $threads=array( );
      $responses=arra y();
      foreach ($hosts as $host)
      $threads=fopen( 'http://domain.with.the .script/path/to/script/
      script.php?op=a ction&host={$ho st}','r');

      foreach ($threads as $thread_id=>$th read)
      if ($thread)
      {
      $responses[$thread_id]=''
      while (!feof($thread) )
      $response[$thread_id].=fgets($thread );
      fclose($thread) ;
      }

      So now the threading part is emulated through Apache, making
      concurrent requests to the script.php that contains the actions to be
      undertaken and specifying the host that I want to be the target of the
      actions.

      Basically that's my approach to multi-threading emulation in PHP.

      So...any suggestions/ideas/anything? :-)

      I am really curious about other approaches because this multi-
      threading thing in PHP seems rather disturbing because of it's
      absence. :-)

      Cheers,
      George Maicovschi.

      Comment

      • Jerry Stuckle

        #4
        Re: PHP Multi-threading

        George Maicovschi wrote:
        Hey Erwin,
        >
        It's just an experiment of mine, nothing production-related... YET :-)
        >
        The thing goes like this. I have a script that's supposed to do an
        action on lets say 100 different hosts in the same time. So I need
        multi-threading ar a way to emulate it. My approach was this:
        >
        $threads=array( );
        $responses=arra y();
        foreach ($hosts as $host)
        $threads=fopen( 'http://domain.with.the .script/path/to/script/
        script.php?op=a ction&host={$ho st}','r');
        >
        foreach ($threads as $thread_id=>$th read)
        if ($thread)
        {
        $responses[$thread_id]=''
        while (!feof($thread) )
        $response[$thread_id].=fgets($thread );
        fclose($thread) ;
        }
        >
        So now the threading part is emulated through Apache, making
        concurrent requests to the script.php that contains the actions to be
        undertaken and specifying the host that I want to be the target of the
        actions.
        >
        Basically that's my approach to multi-threading emulation in PHP.
        >
        So...any suggestions/ideas/anything? :-)
        >
        I am really curious about other approaches because this multi-
        threading thing in PHP seems rather disturbing because of it's
        absence. :-)
        >
        Cheers,
        George Maicovschi.
        >
        Where are you creating new threads?

        I agree with Erwin - if you need to use threads, PHP isn't a good way to
        go. If you really need this and it's has to be in PHP, I'd look at
        spawning multiple processes off and do the work in batch mode, perhaps
        saving the results in a database.

        But personally I'd be looking at another language such as Java or C/C++
        which has good thread support.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • George Maicovschi

          #5
          Re: PHP Multi-threading

          On Mar 31, 5:45 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          George Maicovschi wrote:
          Hey Erwin,
          >
          It's just an experiment of mine, nothing production-related... YET :-)
          >
          The thing goes like this. I have a script that's supposed to do an
          action on lets say 100 different hosts in the same time. So I need
          multi-threading ar a way to emulate it. My approach was this:
          >
          $threads=array( );
          $responses=arra y();
          foreach ($hosts as $host)
          $threads=fopen( 'http://domain.with.the .script/path/to/script/
          script.php?op=a ction&host={$ho st}','r');
          >
          foreach ($threads as $thread_id=>$th read)
          if ($thread)
          {
          $responses[$thread_id]=''
          while (!feof($thread) )
          $response[$thread_id].=fgets($thread );
          fclose($thread) ;
          }
          >
          So now the threading part is emulated through Apache, making
          concurrent requests to the script.php that contains the actions to be
          undertaken and specifying the host that I want to be the target of the
          actions.
          >
          Basically that's my approach to multi-threading emulation in PHP.
          >
          So...any suggestions/ideas/anything? :-)
          >
          I am really curious about other approaches because this multi-
          threading thing in PHP seems rather disturbing because of it's
          absence. :-)
          >
          Cheers,
          George Maicovschi.
          >
          Where are you creating new threads?
          >
          I agree with Erwin - if you need to use threads, PHP isn't a good way to
          go. If you really need this and it's has to be in PHP, I'd look at
          spawning multiple processes off and do the work in batch mode, perhaps
          saving the results in a database.
          >
          But personally I'd be looking at another language such as Java or C/C++
          which has good thread support.
          >
          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstuck...@attgl obal.net
          =============== ===
          Yeah, I would go the C way myself, but it has to be PHP...

          Comment

          • Jerry Stuckle

            #6
            Re: PHP Multi-threading

            George Maicovschi wrote:
            On Mar 31, 5:45 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            >George Maicovschi wrote:
            >>Hey Erwin,
            >>It's just an experiment of mine, nothing production-related... YET :-)
            >>The thing goes like this. I have a script that's supposed to do an
            >>action on lets say 100 different hosts in the same time. So I need
            >>multi-threading ar a way to emulate it. My approach was this:
            >>$threads=arra y();
            >>$responses=ar ray();
            >>foreach ($hosts as $host)
            >> $threads=fopen( 'http://domain.with.the .script/path/to/script/
            >>script.php?op =action&host={$ host}','r');
            >>foreach ($threads as $thread_id=>$th read)
            >> if ($thread)
            >> {
            >> $responses[$thread_id]=''
            >> while (!feof($thread) )
            >> $response[$thread_id].=fgets($thread );
            >> fclose($thread) ;
            >> }
            >>So now the threading part is emulated through Apache, making
            >>concurrent requests to the script.php that contains the actions to be
            >>undertaken and specifying the host that I want to be the target of the
            >>actions.
            >>Basically that's my approach to multi-threading emulation in PHP.
            >>So...any suggestions/ideas/anything? :-)
            >>I am really curious about other approaches because this multi-
            >>threading thing in PHP seems rather disturbing because of it's
            >>absence. :-)
            >>Cheers,
            >>George Maicovschi.
            >Where are you creating new threads?
            >>
            >I agree with Erwin - if you need to use threads, PHP isn't a good way to
            >go. If you really need this and it's has to be in PHP, I'd look at
            >spawning multiple processes off and do the work in batch mode, perhaps
            >saving the results in a database.
            >>
            >But personally I'd be looking at another language such as Java or C/C++
            >which has good thread support.
            >>
            >--
            >============== ====
            >Remove the "x" from my email address
            >Jerry Stuckle
            >JDS Computer Training Corp.
            >jstuck...@attg lobal.net
            >============== ====
            >
            Yeah, I would go the C way myself, but it has to be PHP...
            >
            That's a shame because you're trying to use a hammer when you need a
            screwdriver.

            There really is no *good* way to do it in PHP. About the only way
            you're going to be able to do it is like I suggested above - spawn off
            multiple processes to do the work and collect the results in a database
            or similar.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            Working...