PHP脚本执行和多线程环境(PHP5扩展)
我需要一些关于执行作为 PHP 5 模块实现的“类”的说明。该类有一些方法,包括构造函数/析构函数,在 PHP 脚本中我可以按如下方式使用它:
<?php
// --- construct
$c = new my_class("some argument");
// --- add data
foreach ($arr_of_elements as $k => $v) {
$c->add_element($v);
}
// --- execute algorithm
$c->execute();
// --- get result as xml format
$result = $c->get_result_as_xml();
// --- end of script (destruct)
?>
我的问题是:当发出请求时,上述脚本是在 PHP 内的单线程中执行的Apache2服务器的模块?我认为应该如此。
我问这个问题是因为我用 C 实现了一个 PHP5 扩展,它使用 Java JVM 和一些 JNI 代码,所以当执行上面的脚本时,它会使用类构造函数附加到 JVM,调用方法(实际上是我的 JNI 的包装器)调用)并使用析构函数从 JVM 分离。 在调试模式下执行Apache(使用-X
),脚本运行起来就像一个魅力,多次运行(重新加载)完全没有问题,但Apache2/PHP在常规多进程模式下,经过一段时间后很少有对 JVM 的 JNI AttachCurrentThread
调用挂起。我尝试追踪该问题以找到解决方案。
是否有可能获取我正在执行的线程(id 等)的一些信息?
我需要确保执行是单线程执行。
我在 Ubuntu Lucid 10.04 LTS 上使用 Apache2 和 PHP 5.2.16(从源代码编译)
如果有不清楚的地方,请告诉我。感谢您的信息和帮助!
安德烈亚斯
I need some clarification executing a "class" that is implemented as a PHP 5 module. The class has a few methods including constructor/destructor and in a PHP script I can use it as follows:
<?php
// --- construct
$c = new my_class("some argument");
// --- add data
foreach ($arr_of_elements as $k => $v) {
$c->add_element($v);
}
// --- execute algorithm
$c->execute();
// --- get result as xml format
$result = $c->get_result_as_xml();
// --- end of script (destruct)
?>
My question is: When a request is made is the above script executed in a single thread within the PHP module of the Apache2 server? In my opinion it should.
I am asking this, because I implemented a PHP5 extension in C that uses the Java JVM with some JNI code, so when above script is executed it attaches to the JVM with the class constructor, calls the methods (which are actually wrappers for my JNI Calls) and detaches from the JVM with the destructor.
Executing Apache in debugging mode (using -X
), the and script runs like a charme, multiple runs (reloading) makes no problem at all, but Apache2/PHP in regular multi-process mode, after a few calls the JNI AttachCurrentThread
to the JVM hangs. I try to track down that issue to find a solution.
Is there s possibility to get some information in which thread (id, or such) I am executing?
I need to make sure that the execution is a single thread execution.
I am using Apache2 with PHP 5.2.16 (compiled from source) on Ubuntu Lucid 10.04 LTS
If something is unclear, please let me know. Thanks for the info and help!
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在使用 modphp 的 php prefork() 配置中推荐使用 apahce2。然后每个 http 请求将在其自己的 http 进程中运行,每个进程有一个 php 线程。我怀疑您正在以其他模式之一运行。
如果您不使用 prefork 配置,一些包装 c 库的 php 函数将开始崩溃,因为它们不是线程安全的,包括上面提到的自定义 java 代码。
还有一些 fastcgi 配置的工作方式与 modphp 略有不同。
If you've got apahce2 in the recommended for php prefork() configuration with modphp. Then each http request will be running in its own http process with a single php thread per process. I suspect you are running in one of the other modes.
If your not using prefork configuration a few of the php functions that wrap c libraries will start busting as they are not thread safe, including your custom java code mentioned above.
There are also a couple of fastcgi configurations that work slightly diffrently than modphp.