非阻塞命名管道
问题摘要:我已经成功地加快了上传图像的速度,但代价是使用并发性。现在我需要确保并发性免受竞争条件的影响。我本来打算让从属脚本轮询普通文件以获取独立脚本的状态,但后来决定命名管道会更好。管道以避免轮询并命名,因为我无法从打开它们的脚本中获取 PID(这是我需要使用管道进行通信的管道)。
因此,上传图像时,客户端通过 AJAX 向脚本发送 POST,该脚本 1) 保存图像 2) 生成并行脚本(独立的)来缩略图图像,3) 将有关图像的 JSON 返回给客户端。然后,客户立即请求翻阅版本,希望我们在发送响应时有足够的时间来准备。但如果尚未准备好,Apache mod_rewrites 路径以指向第二个脚本(依赖项),该脚本等待拇指操作完成,然后返回图像数据。
我希望这相当简单,但是,在通过终端单独测试独立脚本时,我得到了:
$ php -f thumb.php -- img=3g1pad.jpg
successSegmentation fault
源代码在这里: http ://codepad.org/JP9wkuba 我怀疑我遇到了段错误,因为我创建的 fifo 仍然打开并且现在是孤立的。但我需要它来让依赖脚本看到,对吗?它不是应该是非阻塞的吗?我想这是因为脚本的其余部分可以运行......但它无法完成?正如我一开始所想的那样,这对于普通文件来说是一项工作,除非两个文件都打开,否则我不想进行轮询。我想最多轮询一次并完成它。我是否只需要进行民意调查并忽略丑陋之处?
Issue summary: I've managed to speed up the thumbing of images upon upload dramatically from what it was, at the cost of using concurrency. Now I need to secure that concurrency against a race condition. I was going to have the dependent script poll normal files for the status of the independent one, but then decided named pipes would be better. Pipes to avoid polling and named because I can't get a PID from the script that opens them (that's the one I need to use the pipes to talk with).
So when an image is uploaded, the client sends a POST via AJAX to a script which 1) saves the image 2) spawns a parallel script (the independent) to thumb the image and 3) returns JSON about the image to the client. The client then immediately requests the thumbed version, which we hopefully had enough time to prepare while the response was being sent. But if it's not ready, Apache mod_rewrites the path to point at a second script (the dependent), which waits for the thumbing to complete and then returns the image data.
I expected this to be fairly straightforward, but, while testing the independent script alone via terminal, I get this:
$ php -f thumb.php -- img=3g1pad.jpg
successSegmentation fault
The source is here: http://codepad.org/JP9wkuba I suspect that I get a segfault because that fifo I made is still open and now orphaned. But I need it there for the dependent script to see, right? And isn't it supposed to be non-blocking? I suppose it is because the rest of the script can run.... but it can't finish? This would be a job for a normal file as I had thought at the start, except if both are open I don't want to be polling. I want to poll once at most and be done with it. Do I just need to poll and ignore the ugliness?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要删除创建的 FIFO 文件,然后完成所有脚本。
You need to delete created FIFO files then finish all scripts.