vpetkar
vpetkar

Reputation: 67

How do you limit the number of people executing a PHP script at once? (Queue, 'spot' system)

I have an upload system ready, however I might be facing a very high load that may bring down our servers. The hosting provider limits the instances of a PHP script running at the same time to about 30. However, while people are uploading they often take longer than expected to finish the script and other people are given an "Internal Server Error" until a spot frees up.

My specific question is, is there a way to limit the number of people running the script at the same time? For example, a queueing system where only 29 people are uploading at the same time (server stays up) and other people can only start uploading when spots free up. How would this be possible?

Upvotes: 3

Views: 218

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Save a value (ideally in Memcache, but MySQL works fine too) that says how many people are uploading. It starts at zero, then when someone starts uploading you increment it before starting to process the upload. When the upload has finished, decrement the counter again.

If the counter is above your threshold, stop people from accessing the uploader or give a friendly error message.

Alternatively, find a host with better systems. I hope this doesn't count as advertising, but the host I use provides much, MUCH better systems, and almost certainly cheaper than whatever you're paying your current host.

Upvotes: 4

diolemo
diolemo

Reputation: 2671

Are you sure they havn't limited the number of apache processes or php processes? If they did there isn't much you can do but ask them to raise the limit. If it truely is a limit on the concurrent executions of a single PHP script then you could work around it as below...

Have lots of scripts that do the exact same thing. They can be symlinks to a single "control" script so that their content is always the same. Then choose one of the scripts at random when presenting the form.

Upvotes: 1

Related Questions