user4672604
user4672604

Reputation:

How Do I load pthreads extension in CLI?

I have built both php and Apache from source on MAC OSX 10.11.

I have been having trouble loading pthreads extension in CLI.. I misunderstood how to load it and now I do not truly understand what should I do.

Here is what I have done so far.

I configured php with the following command:

'./configure' 
'--prefix=/Users/username/Terminal/WebServer'
'--with-apxs2=/Users/username/Terminal/WebServer/bin/apxs'
'--enable-maintainer-zts' 
'--enable-pthreads=shared' 
'--enable-debug' 
'--with-tsrm-pthreads' 
'--with-config-file-path=/Users/username/Terminal/WebServer/ini' 
'--enable-cli'

Apache's loaded Modules:

Compiled in modules:
  core.c
  mod_so.c
  http_core.c
  worker.c

So, in /Users/username/Terminal/WebServer/ini directory I created php-cli.ini file and added extension=pthreads.so line, then I ran php -m command, the following is the output:

[PHP Modules]
Core
ctype
date
dom
fileinfo
filter
hash
iconv
json
libxml
pcre
PDO
pdo_sqlite
Phar
posix
<strong>pthreads</strong>
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter

[Zend Modules]

I restarted Apache and opened index.php file which has phpinfo() in it.
Notable information

Configuration File (php.ini) Path => /Users/username/Terminal/WebServer/ini
Loaded Configuration File => /Users/username/Terminal/WebServer/ini/php.ini
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
Thread Safety => Enabled

phpinfo() doens't include pthreads module information and I don't know whether it should or not.
at this point I open test.php file which includes the following pthreads script:

<?php $thread = new class extends Thread {
    public function run() {
        echo "Hello World\n";
    }
};

$thread->start() && $thread->join();
?>

and the output through apache is:

Fatal error: Class 'Thread' not found in /Users/username/Sites/test.php on line 1


When I run the following command on Terminal
$ php /Users/username/Sites/test.php

The output is : Hello World

So the question is how do I load pthreads extension in CLI properly ?

Upvotes: 1

Views: 2392

Answers (1)

Joe Watkins
Joe Watkins

Reputation: 17158

You already have the desired result, but don't seem to know why ...

The key parts of your configuration are:

configure options

If PHP is being built with Apache DSO SAPI, you must use --enable-pthreads=shared, this causes the build process to create a DSO (shared object, dll in windows speak) from the pthreads library, rather than statically linking pthreads into the Apache DSO.

php.ini configuration

From the PHP manual:

If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini), it is used instead of php.ini. The SAPI name can be determined with php_sapi_name().

Having built pthreads shared (pthreads.so), we can simply create php-cli.ini in the same path as php.ini and add extension=pthreads.so to it.


What you have works, however, it's not ideal; You don't really want Apache to use a PHP interpreter that has the overhead of ZTS.

Ideally, you will want to build and install PHP twice:

First Build

configure --with-apxs=... --disable-pthreads --prefix=/system/prefix --with-config-file-path=/system/prefix/etc ...

Replace /system/prefix/ with sensible system prefix like /usr/ ...

When you make install this build, you will create the ideal installation for Apache, which includes a non-thread safe PHP binary at /system/prefix/bin/php, with all related scripts (pecl, phpize, php-config etc.) in /system/prefix/bin.

Second Build

Before this build you need to make clean (if you are using a release tarball), or vcsclean (if you are using local git repository).

configure --enable-maintainer-zts --enable-pthreads=static --prefix=/special/prefix --with-config-file-path=/special/prefix/etc ...

Replace /special/prefix/ with sensible special prefix like /opt/, do not use the same prefix as the first build ...

When you make install the first installation is not touched, instead a new installation is created without support for Apache, but with support for pthreads built statically into the binary (configuration free).

The binary will be at /special/prefix/bin/php with all related scripts in /special/prefix/bin.

Wiring Builds Together

If both builds require the same PECL extensions with the exception of pthreads, you can use the same --with-config-file-scan-dir configure switch for both builds.

If you are really brave, you could use the same --with-config-file-path switch too, but this might have undesired results.

Editing include_path for both builds will also help.

Upvotes: 2

Related Questions