PHP 线程安全吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/681081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:28:50  来源:igfitidea点击:

Is PHP thread-safe?

phpmultithreadingthread-safety

提问by vartec

  • Is PHP (as of 5.2) thread-safe on Linux/UNIX?
  • Would it be possible to use it with Apache Worker-MPM or Event-MPM?
  • PHP(从 5.2 开始)在 Linux/UNIX 上是线程安全的吗?
  • 是否可以将它与 Apache Worker-MPM 或 Event-MPM 一起使用?

The facts I gathered so far are inconclusive:

到目前为止,我收集到的事实尚无定论:

  • Default binaries included in most distributions have ZTS disabled, so I'm aware, that I'd have to recompile them.
  • In theory Zend Engine (core PHP) with ZTS enabled is thread-safe.
  • It's said that some modules might not be thread-safe, but I haven't found any list of modules that are or that are not.
  • PHP FAQstates pretty much same as above.
  • 大多数发行版中包含的默认二进制文件都禁用了 ZTS,所以我知道我必须重新编译它们。
  • 理论上启用 ZTS 的 Zend 引擎(核心 PHP)是线程安全的。
  • 据说有些模块可能不是线程安全的,但我没有找到任何模块列表。
  • PHP FAQ 的陈述与上面的几乎相同。

What's your experience?

你有什么经验?

It's not only about segmentation faults("access violations" in Windows nomenclature). There is a lot more to thread safety.

这不仅仅是关于分段错误(Windows 术语中的“访问冲突”)。线程安全还有很多。

采纳答案by OIS

I know gettext and set_localeis not threadsafe. PHP should not be used with a threaded MPM.

我知道 gettext 和set_locale不是线程安全的。PHP 不应与线程 MPM 一起使用。

PHP Isn't Thread-Safe Yet.
Running PHP not threaded.

PHP 还不是线程安全的
运行 PHP 未线程化

回答by AbdolHosein

See Where can I get libraries needed to compile some of the optional PHP extensions?for a list of thread-safe and nonthread-safe extensions (* marked are not thread-safe and others are).

请参阅从哪里可以获得编译某些可选 PHP 扩展所需的库?获取线程安全和非线程安全扩展的列表(* 标记的不是线程安全的,其他的则是)。

回答by Tim Post

A better question might be, "Is the following PHP code going to trigger access violations if MPM is used?" Or, "Have you experienced odd behavior likely attributed to concurrency issues using the following functions?"

一个更好的问题可能是,“如果使用 MPM,以下 PHP 代码是否会触发访问冲突?” 或者,“您是否遇到过可能归因于使用以下函数的并发问题的奇怪行为?”

Otherwise, it's Russian roulette. If you're using some packaged application, it may work just fine now but break a month from now when a new version of the application comes out.

否则,就是俄罗斯轮盘赌。如果你正在使用一些打包的应用程序,它现在可能工作得很好,但是当应用程序的新版本出现时,它会在一个月后中断。

I strongly advise against using MPM with PHP in general. However, if you have some small code to run, you could post it, and we could tell you if you're going to hit a pitfall.

我强烈建议一般不要在 PHP 中使用 MPM。但是,如果您有一些小代码要运行,您可以发布它,我们可以告诉您是否会遇到陷阱。