Multitasking in PHP

Update: Old Technical Post. The findings, information and discussion might be outdated, useless or plain wrong given the current and fluid area of the topic in this post.

Technically, the answer is simply forking scripts or if multithreading is needed, using another language. It is okay, to learn another language and use it where one language might fail.

Common Developer Question

The misconception about PHP is that it doesn’t offer multitasking and that it is impossible. It is true that PHP doesn’t allow the userland developers access to multithreading features that Java enjoys. There is a reason for this and it will become clear.

Forking is one method of multitasking and there exists an extension called Process Control Extension to handle this. There are also command line PHP commands that will allow for an process to run outside of PHP and return the output to PHP.

Multithreading in PHP has little merits for developers. In most cases, the script should only run for a few milliseconds and after that time it will be dismantled. A poorly designed script that used multiple threads would further lengthen the time for which the script would run giving no advantage. There is no need, for example, to handle both GUI responsiveness and uploading to a file server in PHP using a web SAPI.

The primary purpose of PHP has always been for web development and there shouldn’t be a need for having two threads running concurrently. All you should be doing is processing for output. The design method of PHP web development differs from that of desktop applications. To put bluntly, creating another thread is not going to solve your performance problem.

Simulating Multitasking

The Process Control extension isn’t meant for web servers and so you have to fake multitasking to the visitor. There are many ways to do this, but what are doing is mostly caching the results for the visitors.

  1. Cron Jobs for time consuming tasks that “save” to another location, either file or database.
  2. Processing and caching for one request and then using the cached file for every request after that.
  3. JavaScript XMLHttpRequest or AJAX as it is commonly known for collecting data after the server side script has been sent.

Learning Curve for Multithreading

Multithreading is difficult, and to offer it to beginners would be asking for a lot of Apache servers to crash. Even if PHP abstracted and kept tracked of deadlocks and race conditions, which would be somewhat difficult, it would add overhead to the compiling and execution. It is best to leave the multithreading to Apache and PHP and not userland.

Race Conditions

Race Conditions occur whenever threads are dependent on accessing the same resource and by preforming actions that cancel out the other thread. Which is why it is better to use a database for storing data instead of a file for both reading and writing from two different scripts.

Dead Locks

A dead lock is whenever one Thread B locks a resource waiting for another Thread A to preform its action. Thread A needs the resource that Thread B has locked, so it can’t preform its task until Thread B releases the resource. It is an never ending cycle that is hell, from what I have read. They can be avoided by only locking for a short interval.

Resources

Cameron and Tracy Hughes. Object-Oriented Mulithreading Using C++. John Wiley and Sons. 1997.

Possibly Related Posts:


7 Comments.

  1. I agree. Some could argue that problems which require multi-threading also require a different tool, but I think PHP users wouldn’t lose anything with MT support. They would rarely gain anything, too, but this is irrelevant.

  2. also, multithreading support would make it possible to create really efficient application-servers (fastcgi-based, for example)

  3. php already supports fork. and you can do shared memory through memory mapped file io — although it is a bit clumsey due to the lack of a locking mechanisim, you must roll your own which is a lot fo work.

    So, I don’t understand what it is that you think needs to be added. it already does this. Your saying you want to do multitasking inside of apache and pcntl doesn’t work? well, whats wrong with exec mychild… and pass it a db pointer or redirected io? it accomplishes the same thing as fork but with a little more overhead.

    When inside of a web page context, you should be doing as little as possible anyway. Get in generate html and get out…. But cli is a whole different beast and many people are coding app servers, just look through the manual for examples or irc servers etc.

    If you want the php coders to consider any of this, First you need to explain clearly… just what it is that you want.
    AND then explain how it is not already possible to do what you think you want to do. I suspect you are going to be very surprised to find that with a little thought you can already do everything that you are asking for.

    I myself have written a cli multi-tasking server and the only limit I found was that after a child has dropped it’s privledges it can not send signals to it’s parent. that is a pain… but there are some simple work-arounds. and I suppose if I ever spent a bunch of time figuring out the poorly documented group and session ownership permissions that there is actually a way to do it. but for me it was far easier to reapproach the problem in a different way. There are many ways to do IPC, but after thinking it through I realised that the correct solution was for the parent to do less and put more of the task in the child, that way the child had no need to talk to the parent.

    Focus on goals not techniques and you can usually find a way to finagle anything that you want to acomplish.

  4. Multithreading = Multiple Threads inside of the same Process using the same memory space.

    Multitasking = Multiple Processes working together with separate memory space. This includes forking and the PHP extension.

    I already stated that multitasking is possible and stated what extension to use.

    What I was trying to discuss is abstracting the creation of multithreading to allow for Action Oriented programming. I was going to write a post about it, but I read the Extending and Embedding PHP book and found out a lot of things about PHP. I came to realize that someone like that wouldn’t be acceptable.

    The primary focus of this post is for future reference. Instead of discussing in forums how multitasking is possible, I can just link to this post.

  5. http://dev.pedemont.com/sonic/ cli or pseudo cgi forks made a little more simple for php scripting

Trackbacks and Pingbacks: