Can't stop making
Home

Multi-threading in PHP

You can have multi-threading in PHP by using exec to call a different script and pipe it's output elsewhere. This is basically simulating multi-thread support as PHP itself does not have it and thus it won't have convinent methods and handles to deal with threads.

So for example:

exec('php thread2.php > /dev/null &');

Would execute thread2.php in a seperate thread and send output to null. You can of course also pipe outputs to file to exchange data between threads via files. (Probably better to use memory)

You can pass arguments by putting the arguments after the script file name:

exec('php thread2.php 1 test > /dev/null &');

In thread2.php you can access the arguments with global variables $argv and $argc.

More details here: http://www.php.net/manual/en/features.commandline.usage.php

標籤 tag(s)

  • PHP

Version

1.0

Last Updated

12th October, 2011

First Published

12th October, 2011

Comments

Leave a comment

Any published comments will adhere to the etiquette policy and all e-mail addresses will be treated in accordance to the privacy policy.

  • If you see this, something's wrong with Twitter.

Blatant Plug

Need a new CPU or graphics card? Get AMD.

AMD Vision and AMD Radeon Graphics

Support

I've pledged my support to these organisations, and I think you should too.

  • Google+
  • Follow ronaldslc on Twitter
  • View Ronald Chan's profile on LinkedIn