How to get pthreads working in PHP?
Solution 1
I've noticed that wampserver has php.ini in two separate places. One place is in the /wamp/bin/php/php5... directory, and the other place is in the /wamp/bin/apache/apache.../bin directory (where "..." represents version numbers). The two files need to be identical, because apparently both are loaded at different times by the overall wampserver boot-up procedure.
(Note I only discovered this recently, and may be well "behind the curve" of doing fancy things with wampserver --maybe everyone else has been dealing with both files for a long time. So I don't know if this simple thing will fix your problem; I came here looking for info, myself, regarding doing some multi-threading stuff. :)
One other thing. According to this page: www.php.net/manual/en/pthreads.requirements.php PHP has to be compiled with "--enable-zts" in order for pthreads stuff to work. I have not been able to find any evidence that the PHP part of wampserver was compiled that way.
(months later) Having decided I didn't really immediately need to do any threading stuff, I went on to do other things, until the need actually arose. I now can say that the version of PHP compiled into WampServer does support the "pthread" extension, although some set-up work is needed, first. The instructions I saw mentioned putting a couple of .dll files (after a download and unZip) into certain places, but that didn't work for me. Copying them to the \Windows\System32 directory did work. (Putting them into the \apache...\bin directory also works; there are some other php .dll files in there.)
After that, much like what you did, it is necessary to define a "class" that extends the "Thread" class, in order to actually do something in another thread. The "run()" function in the Thread class is "abstract", and needs to be "realized" as an actual function in the extended class. Then the "new" operator can create an "instance", an object of that specified class, for actual use. Here's the class that I needed:
//Purpose: Use another thread to run the code in another php file, after a delay
class xT extends Thread
{ var $fil, $tim;
function savWhatWhen($f="", $t=0)
{ $this->fil = $f; //save What, file to process
$this->tim = $t; //save When, delay before processing file
return;
}
function run()
{ ini_set('max_execution_time', 600); //600 seconds = 10 minutes
sleep($this->tim); //do a delay; beware of max-exec-time!
include($this->fil); //load file-to-process, and process it
return;
} }
That "savWhatWhen()" function was created specifically for this extension of the basic Thread class. Here's some code for using that class:
$TH = new xT(); //prepare a separate Thread
$TH->savWhatWhen("d:/wamp/myscripts/test.php", 45);//file-name and delay time
$TH->start(); //after delay, process file
//the code that does this can terminate, while OTHER thread is doing a delay
Note for anyone copying this code, you might need to make sure your "open_basedir" setting in the php.ini allows access to the specified file.
More months later: With lots of things being worked on, I haven't put a lot of time into using my pthread object. I did encounter a peculiarity that makes me wonder about whether or not I can actually use pthreads the way I had hoped. Here is what I have observed: 1. An initial php file is called by AJAX, to do something. 2. The PHP processor on the Web Server does that thing. 3. Various data is supposed to be echoed to the browser. 4. The initial php file calls for the creation of another thread, and terminates. 5. The browser does not yet receive the echoed data! 6. The PHP processor on the Web Server does the work delegated to the second thread. 7. When the second thread terminates, NOW the browser receives the echoed data!
At this writing I'm thinking I missed something. Perhaps I need to do some forceful "flush" stuff when the first thread ends, so that the browser can receive the echoed data and the user can do things while the PHP processor on the server is also doing things.
Solution 2
I've encountered the same problem, in my case placing the pthreadVC2.dll
in
..wamp\bin\apache\Apache2.4.4\bin
(instead of ..\wamp\bin\php\php5.4.16
as the guide in php.net instructs) solved the problem
Solution 3
Check for extension_dir = "ext"
in you php.ini file. Make sure it points to the folder where your extensions reside and make sure it's not commented (it has a semicolon ;
in front of it)
Solution 4
You have to add a require_once() with the path of the Thread class before extending it (if your framework don't use an autoload class system)
Justin k
Updated on June 05, 2022Comments
-
Justin k almost 2 years
I am using wampserver to test & run wordpress code in my local computer. In order to run pthread, I have followed the following steps:
1) I got the pthread zip file from http://windows.php.net/downloads/pecl/releases/pthreads/0.44/ (My machine has php 5.3.13 and downloaded the
php_pthreads-0.44-5.3-ts-vc9-x86.zip file
from the above link).2) Extracted the zip file. Moved the php_pthreads.dll to the
C:\wamp\bin\php\php5.3.13\ext
directory.3) Moved pthreadVC2.dll to the
C:\wamp\bin\php\php5.3.13
directory.4) Then Opened
C:\wamp\bin\php\php5.3.13\php.ini
and added the codeextension=php_pthreads.dll
at the begining of the file.But when I try to run the following code:
<?php class My extends Thread { public function run() { printf("%s is Thread #%lu\n", __CLASS__, $this->getThreadId()); } } $my = new My(); $my->start(); ?>
It gives me the following error:
Fatal error: Class 'Thread' not found in C:\wamp\www\wp-admin\includes\post.php on line 2
Can you please tell me how to install pthreads in my computer to use with php? and do I have to install any other software?
-
kqlambert over 10 yearsWas wondering if you ever solved this problem as I am getting the same error
-
Nicolas Racine about 10 yearsYea me too same problem :/
-
Kyohei Kaneko almost 10 yearsSee my answer below on how to correctly get pthreads working with wamp server
-
-
Justin k almost 11 yearsIt is pointing to the right folder
extension_dir = "c:/wamp/bin/php/php5.3.13/ext/"
bellow the code I have put underneeth that line:extension=php_pthreads.dll
Can you please tell me if I have downloaded the correct zip file for my php 5.3.13 version? if not, please tell me which one is it from this list:http://windows.php.net/downloads/pecl/releases/pthreads/
-
marlonp33 almost 11 yearsThe given path is relative from your PHP directory, so probably it should be just "ext". I'm not sure if it matters, but try to move
extension=php_pthreads.dll
to the section where the other extensions are declared (do a 'find'-search forextension=
). -
Sunny over 10 yearsC:\wamp\bin\php\php5.3.13\php.ini is for cli version but for web we have to edit C:\wamp\bin\apache\apache2.2.22\php.ini
-
Kyohei Kaneko almost 10 yearsWamp server does autoload classes, but has a seperate php.ini file for browser and cli.
-
Kyohei Kaneko almost 10 years@Sunny Agreed! See my answer below for more details
-
1000Gbps almost 8 yearsSome 7.x versions of PHP don't work with 3.1.6, both x86 and x64. There' s a mismatch between module API versions, used for building of PHP and pthreads. Also seems there isn't a good walkthrough how to build it.