如何在CLI中加载pthreads扩展? [英] How Do I load pthreads extension in CLI?

查看:71
本文介绍了如何在CLI中加载pthreads扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在MAC OSX 10.11上从源代码构建了php和Apache.

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

我在CLI中加载pthreads扩展时遇到了麻烦.我误解了如何加载它,现在我并没有真正理解应该怎么做.

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.

我使用以下命令配置了php:

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的已加载模块:

Apache's loaded Modules:

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

因此,在/Users/username/Terminal/WebServer/ini目录中,我创建了 php-cli.ini 文件,并添加了 extension = pthreads.so 行,然后我运行 php -m 命令,输出如下:

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]

我重新启动了Apache,并打开了其中包含 phpinfo() index.php 文件.
重要信息

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

配置文件(php.ini)路径=>/Users/用户名/Terminal/WebServer/ini
已加载的配置文件=>/Users/用户名/Terminal/WebServer/ini/php.ini
扫描此目录以查找其他.ini文件=>(无)
已解析的其他.ini文件=>(无)
线程安全=>启用

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()不包含pthreads模块信息,我不知道是否应该.
此时,我打开包含以下pthreads脚本的test.php文件:

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();
?>

并且通过apache的输出是:

and the output through apache is:

致命错误:在第1行的/Users/username/Sites/test.php中找不到类'Thread'


当我在Terminal上运行以下命令时
$ php/Users/username/Sites/test.php

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

输出为: Hello World

所以问题是如何在CLI中正确加载pthreads扩展?

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

推荐答案

您已经有了理想的结果,但是似乎不知道为什么...

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

配置的关键部分是:

如果PHP是使用Apache DSO SAPI构建的,则必须使用-enable-pthreads = shared ,这将导致构建过程从以下位置创建DSO(共享对象,Windows中的dll):pthreads库,而不是将pthread静态链接到Apache DSO.

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 手册:

如果存在php-SAPI.ini(其中SAPI是使用中的SAPI,例如php-cli.ini或php-apache.ini),则使用它代替php.ini.可以使用php_sapi_name()确定SAPI名称.

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().

具有共享的内置pthreads(pthreads.so),我们可以在与 php.ini 相同的路径中简单地创建 php-cli.ini 并添加 extension= pthreads.so .

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.

您的作品行之有效,但这并不理想;您真的不希望Apache使用具有ZTS开销的PHP解释器.

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.

理想情况下,您将需要两次构建和安装PHP:

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

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

用合理的系统前缀(例如/usr/ ...

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

在此版本中进行安装时,将为Apache创建理想的安装,其中包括位于/system/prefix/bin/php ,以及/system/prefix/bin 中的所有相关脚本( pecl phpize php-config 等).代码>.

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.

在进行此构建之前,您需要进行 clean (如果使用的是发行版tarball)或 vcsclean (如果使用的是本地git存储库).

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 ...

用合理的特殊前缀(例如/opt/)替换/special/prefix/,请勿使用与第一个版本相同的前缀...

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

当您进行 make install 时,不涉及第一个安装,而是创建了一个新安装,不支持Apache,但支持静态内置到二进制文件中的pthread(免费配置).

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).

二进制文件将位于/special/prefix/bin/php 中,所有相关脚本均位于/special/prefix/bin 中.

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

如果两个构建都需要相同的PECL扩展,但pthread除外,则可以对两个构建使用相同的-with-config-file-scan-dir 配置开关.

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.

如果您真的很勇敢,也可以使用相同的-with-config-file-path 开关,但这可能会产生不希望的结果.

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

为这两个版本编辑 include_path 也将有所帮助.

Editing include_path for both builds will also help.

这篇关于如何在CLI中加载pthreads扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆