7

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Grault
  • 974
  • 12
  • 31
  • 3
    I realize this won't directly answer your question, but why did you choose this *incredibly bizarre* route rather than using a [work/message queue like Gearman](http://gearman.org/)? – Charles Mar 15 '11 at 20:33
  • @Charles: Hm, I had heard of that but didn't know exactly what it did. But I really don't need yet another API to learn at the moment. – Grault Mar 15 '11 at 21:41
  • 8
    The 2-4 methods you'll need to worry about are trivial to learn compared to the effort needed to dig yourself out of the crashtastic named pipe quagmire you've found yourself in. :) Gearman even lets you [send status data back while processing](http://www.php.net/manual/en/gearmanjob.sendstatus.php), something you seem to have a good case for here. (Sending status information back actually significantly increases the complexity of the job retrieval process -- it goes from fire-and-forget / fire-and-wait to fire-and-loop-while-processing-flags.) – Charles Mar 15 '11 at 22:39
  • 1
    Agreed, gearman's worth a look, or php-resque might be a little simpler for you (used both with good success) – Ian Selby Mar 15 '11 at 22:51
  • @Charles why not to make your comment an answer? I see it is highly rated enough for that ;) – Vladislav Rastrusny Mar 28 '11 at 10:04
  • 2
    @FractalizeR, I've found that "Do this instead, you're insane" doesn't always go over well as an actual *answer*, though it is appropriate in this case. I may well do an answer conversion after all. – Charles Mar 28 '11 at 15:35

1 Answers1

1

You need to delete created FIFO files then finish all scripts.

Liutas
  • 5,547
  • 4
  • 23
  • 22