Introduction
pthreads is an Object Orientated API that allows user-land multi-threading in PHP. It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Stackables. Read More
Requirements
- PHP 5.3+ (5.4) x64 or x86 - Thread Safe (Releases)
- Apache httpd (Apache HTTP Server Project) (Releases) Optional
- pthread-w32 library (Releases)
How to configure from scratch step by step
In this tutorial i'm using:
In this tutorial i'm using:
- php-5.4.13-Win32-VC9-x86
- pthreads-0.42-5.4-ts-vc9-x86
Root directory |
3. Copy "php_pthreads.dll" from "pthreads-0.42-5.4-ts-vc9-x86" Folder to "php-5.4.13-Win32-VC9-x86\ext" Folder
4. Find and rename "php.ini-development" file to "php.ini" in "php-5.4.13-Win32-VC9-x86" Folder
5. Open "php.ini" file with notepad or notepad++ and fine following text ";extension=php_shmop.dll" and add this configuration after it "extension=php_pthreads.dll" should look like:
...
;extension=php_pgsql.dll
;extension=php_pspell.dll
;extension=php_shmop.dll
extension=php_pthreads.dll
6. Find and remove the comment symbol ";" " ; extension_dir = "ext" " should look like:
; extension_dir = "ext" => extension_dir = "ext"7. Save the "php.ini" file and close it.
8. Create "index.php" on your desktop and open with notepad or notepad++.
9. Place this code
<?php
class AsyncOperation extends Thread {
public function __construct($arg){
$this->arg = $arg;
}
public function run(){
if($this->arg){
for($i = 0; $i < 5; $i++) {
echo "-> " . $this->arg . "\n";
sleep(1);
}
}
}
}
flush();
$thread = new AsyncOperation("Thread 1");
$thread2 = new AsyncOperation("Thread 2");
$thread->start();
$thread2->start();
$thread->join();
$thread2->join();
?>
10. Save the file and close the editor (notepad / ++)
11. Open CMD (Command Prompt) and execute the following line:
- cd "C:\Users\ USER \Desktop\apachePhp\php-5.4.13-Win32-VC9-x86"
- php -q C:\Users\ USER \Desktop\index.php
12. Result should be like this:
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
Could you please provide an example on how to run a pthreads script from the browser, on windows?
ReplyDeleteyou should be able to run same script in browser over apache, it should work but i never tested it, i will try and will get back with more info for you.
DeleteVery Nice Tutorial Please if you can also provide an suggestions on how to run a pthreads script from the browser using wamp/xampp server localhost, on windows?
ReplyDeleteI will make another post on the official web site (phplobby.com) about that. Thanks for suggestion and feedback.
DeleteFatal error: Class 'Thread' not found in F:\xampp\htdocs\loc\test.php on line 2
ReplyDelete