简体   繁体   English

如何同时运行多个 php 文件?

[英]how to run multiple php files at the same time?

build environment nginx/1.18.0 (Ubuntu 20.04) / php7.4-fpm搭建环境 nginx/1.18.0 (Ubuntu 20.04) / php7.4-fpm

i'm confused about php multi-threading capabilities, i know that it doesn't support it in stock build and it requires a PHP build with ZTS (Zend Thread Safety) to go with pthreads or parallel i'm confused about php multi-threading capabilities, i know that it doesn't support it in stock build and it requires a PHP build with ZTS (Zend Thread Safety) to go with pthreads or parallel

pthreads seems experimental, unsafe and not fit for server environment so that leaves parallel as for the more suitable option pthreads 似乎是实验性的、不安全的并且不适合服务器环境,因此保留并行作为更合适的选项

what i'm trying to accomplish isn't complicated, i just want the server to run a query every second or so looking for new data in a mysql database to serve it through a phpwebsocket without the server halting everything when it starts the query loop because php can't chew gum and walk at the same time.我想要完成的并不复杂,我只是希望服务器每秒运行一次查询,在 mysql 数据库中寻找新数据,通过 phpwebsocket 提供服务,而服务器在启动查询循环时不会停止一切因为 php 不能一边嚼口香糖一边走路。

currently if client 1 loads php page A, he can't access any other pages until page A is done running, yet other clients can still access other pages when client 1 is still running page A which makes me think that it is single threaded per session.目前,如果客户端 1 加载 php 页面 A,在页面 A 完成运行之前,他无法访问任何其他页面,但是当客户端 1 仍在运行页面 A 时,其他客户端仍然可以访问其他页面,这让我认为它是单线程的session。

the alternative is having the client side doing it with repeating ajax request, but that seems to be bandwidth wasting and inefficient.另一种方法是让客户端重复 ajax 请求,但这似乎浪费带宽且效率低下。

I think you have misunderstood what's happening.我想你误解了正在发生的事情。 You don't need multi-threading to serve multiple web pages at once, and keeping a process running to serve a websocket connection isn't going to make everything else grind to a halt.您不需要多线程来同时为多个 web 页面提供服务,并且保持进程运行以服务 websocket 连接不会使其他一切都停止。

currently if client 1 loads php page A, he can't access any other pages until page A is done running, yet other clients can still access other pages when client 1 is still running page A which makes me think that it is single threaded per session.目前,如果客户端 1 加载 php 页面 A,在页面 A 完成运行之前,他无法访问任何其他页面,但是当客户端 1 仍在运行页面 A 时,其他客户端仍然可以访问其他页面,这让我认为它是单线程的session。

The word "session" here is probably the key to your actual problem: PHP's session support uses exclusive locking by default.这里的“会话”一词可能是您实际问题的关键:PHP 的session 支持默认使用独占锁定。 When you call session_start() to load an existing session, the default implementation reads a file on disk, and locks it until the end of the request or until you call session_write_close() .当您调用session_start()加载现有的 session 时,默认实现会读取磁盘上的文件,并将其锁定直到请求结束或直到您调用session_write_close() Other attempts to read the same session data will wait until the lock is released.其他读取相同 session 数据的尝试将等到锁被释放。 This prevents corruption caused by different requests writing conflicting versions of the same session, but it means that the user will see a delay if they open multiple pages in parallel.这可以防止由于不同请求写入相同 session 的冲突版本而导致的损坏,但这意味着如果他们并行打开多个页面,用户将看到延迟。

The solution is simple: if you have a long-running request, don't run session_start() , or if you need to read the session, immediately run session_write_close() to unlock it for use by other requests.解决方法很简单:如果你有一个长时间运行的请求,不要运行session_start() ,或者如果你需要读取 session,立即运行session_write_close()解锁它以供其他请求使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM