为什么 PHP 有单独版本的 ts(线程安全)/nts(非线程安全)而不支持多线程? [英] Why PHP has separate version of ts(thread safe)/nts(non thread safe) while it doesn't support multi-threading?

查看:35
本文介绍了为什么 PHP 有单独版本的 ts(线程安全)/nts(非线程安全)而不支持多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得标题很清楚.

推荐答案

虽然您无法从 PHP 代码生成线程,但您可以将 PHP 与多线程 Web 服务器一起使用,该服务器处理不同线程上的并发请求.在这种情况下,应使用 PHP 的 TS(线程安全)版本.

While you can't spawn threads from PHP code you can use PHP with a multi-threaded web server that handles concurrent requests on different threads. In this case the TS (thread-safe) version of PHP should be used.

TS 版本的 PHP 将每个请求的状态保存在自己的内存位置.这是必要的,因为多线程服务器中的所有请求共享相同的地址空间.

The TS version of PHP keeps the state of each request in its own memory location. This is necessary because all requests in a multi-threaded server share the same address space.

另一种方法是使用多进程(通常是 prefork)服务器.有了这样的服务器,一些状态可以保存在全局变量中,而不会影响并发请求.这就是 PHP 的 NTS(非线程安全)版本的实现方式.

The alternative is to use a multi-process (usually prefork) server. With such a server some state can be kept in global variables without affecting concurrent requests. That's how the NTS (non thread-safe) version of PHP is implemented.

这篇关于为什么 PHP 有单独版本的 ts(线程安全)/nts(非线程安全)而不支持多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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