Multithreading for standalone php

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

    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
  • Chris Hope

    #2
    Re: Multithreading for standalone php

    aladdin wrote:
    [color=blue]
    > 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?[/color]

    You can fork a process but not do multi-threading.

    More info here:



    --
    Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

    Comment

    • Jerry Stuckle

      #3
      Re: Multithreading for standalone php

      aladdin wrote:[color=blue]
      > 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?
      >[/color]

      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.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • tony@tony.com

        #4
        Re: Multithreading for standalone php

        In article <NOSdnYkKFc-0_PXZnZ2dnUVZ_v WdnZ2d@comcast. com>,
        jstucklex@attgl obal.net says...
        [color=blue]
        >
        > 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.
        >[/color]

        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

        Comment

        • Jerry Stuckle

          #5
          Re: Multithreading for standalone php

          tony@tony.com wrote:[color=blue]
          > In article <NOSdnYkKFc-0_PXZnZ2dnUVZ_v WdnZ2d@comcast. com>,
          > jstucklex@attgl obal.net says...
          >
          >[color=green]
          >>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.
          >>[/color]
          >
          >
          > 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[/color]

          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.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Chung Leong

            #6
            Re: Multithreading for standalone php

            tony@tony.com wrote:[color=blue]
            > 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[/color]

            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.

            Comment

            Working...