4

I suspect this question will seem too... silly, but I'm trying to get my head around a nice solution and I'm kinda stuck.

So, here's my situation :

  • I'm using Ajax to perform a series of tasks. Actually, I'm queuing (or parallely at times, doesn't really matter to me) 1 or more requests.
  • Show the progress as a percentage (1 out of X tasks performed)
  • When finished, show the final result.

What I'm trying to do :

  • Instead of having 3-4 different tasks running (= 3-4 different PHP scripts called asynchronously via Ajax), I would like to have just 1 (= 1 script) - in other words, combine the X scripts into one. (That's easy).

Issues I'm facing :

  • How could I still report the percentage complete (1 out of X tasks)?

Any ideas?

4 Answers 4

3

I would have updated a key in Memcached each time a task is complete. And then you would let your ajax-call simply get the value from your memcached key.

http://php.net/manual/en/book.memcached.php

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm... This seems like an interesting workaround. I'll have a look into it. Thanks!
1

If you have the request dropped into a database by your original ajax, then kick off the script, you will still have time (assuming the request takes some time to complete) for subsequent tasks to be dropped into the same database to be picked up by the script that is still running.

As for reporting it, perhaps run a quick jquery to see how many items are in the queue? Alternately have the task.php file update the database (possibly another table even) to say how many jobs it has completed and how many are currently still in the queue.

1 Comment

Hmmm... I've thought of something like that - only with using a file as a queue holder... I might give it a try anyway... Thanks for the idea!
1

If you don't need to send much data to the PHP script, you can use a "long poll" approach. With this, you don't use AJAX but insert a script tag like this:

<script src="my_php_script?task1=x&param_t1_1=42&task2=y"></script>

The PHP file can then send back a JavaScript command like

updatePercent(12);

after each task is done. The commands should be executed by the browser whenever they come in. Be sure to call flush after every task.

Looking into Comet may give you other ideas on how to handle the Client-Server connection.

Comments

0

You can manage queue - before sending AJAX request - put the task in queue (could be object or array). Run AJAX asynchronously with complete function which will remove a job from queue when it's done. You can update progress together with removing job from queue or handle it separately using setTimeout() which will check how many task there are in queue and how many were put in it in total: % = (submitted_tasks - items_in_queue) / submitted_tasks * 100

2 Comments

Pavel, that's what I'm currently doing; what I want is 1 ajax request = 1 task = 1 php script (performing multiple things, but still reporting on the progress of sub-routines)
I don't think you can monitor the progress with 1 script since you need to know how much have been done - you can't do it in PHP script since ECHOing it will terminate request. What about monitoring script which will be called to determine how much have been done, for example how many rows in DB were updated/inserted/deleted relative to total amount of job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.