3

This is a bit complicated, so please don't jump to conclusions, feel free to ask about anything that is not clear enough.

Basically, I have a websocket server written in PHP. Please note that websocket messages are asynchronous, that is, a response to a request might take a lot of time, all the while the client keeps on working (if applicable).

Clients are supposed to ask the server for access to files on other servers. This can be an FTP service, or Dropbox, for the matter.

Here, please take note of two issues: connections should be shared and reused and the server actually 'freezes' while it does its work, hence any requests are processed after the server has 'unfrozen'.

Therefore, I thought, why not offload file access (which is what freezes the server) to PHP threads?

The problem here is twofold;

  • how do I make a connection resource in the main thread (the server) available to the sub threads (not possible with the above threading model)?
  • what would happen if two threads end up needing the same resource? It's perfectly fine if one is locked until the other one finishes, but we still need to figure out issue #1.

Perhaps my train of thought is all screwed up, if you can find a better solution, I'm eager to hear it out. I've also had the idea of having a PHP thread hosting a connection resource, but it's pretty memory intensive.

4

1 回答 1

5

PHP 不支持线程。PHP 的目的是快速响应 Web 请求。这就是构建架构的目的。不同的库尝试做类似线程的事情,但它们通常会导致比它们解决的问题更多的问题。

一般来说,有两种方法可以实现您想要的:

于 2012-05-13T23:27:46.807 回答