Hello, gentlemen.

There is a small application, and the problem is resource-intensive. The fact is that if it is used by more than n people at the same time, it will work incorrectly, and therefore the question arose: how to control the use of resources with PHP, and not with the server settings?

For example, in order to be able to spend n resources. At the moment, I see only the following option:

When the application starts, write +1 to the configuration file, and -1 at the end of the operation. And upon reaching, say, 10 launches at the same time, the user is prevented from starting applications by reporting the queue.

  • Does the server ask you every second for what? Or scripts run on the server with the set_time_limit(0); setting set_time_limit(0); ? - Visman
  • each such attempt to write +1 and -1 ultimately rests on "how to know that the user" finished the work. So the course of thought is correct, it is necessary to count how many active users, another question is how to do it. But in general, it is interesting that you have such a server does a labor-intensive. - teran

1 answer 1

For example, you have a daemon that sends a huge number of requests to some external resources and writes the results to the database. Suppose the work of such a demon lasts from 1 minute to 20 minutes. And it is called in the crown every minute. How to make so that at one time only one demon worked?

To avoid such situations, when the same message is sent several times, you need to store somewhere the status of the daemon, whether it is running at a given time or not (in the database, in files). Each time before starting the daemon, it is necessary to check whether it is running, if yes, then not to run.

Now particular.

If we do through the database: then the demons table looks like from 3 columns: the name of the daemon (name), the number of running demons (runnig_count), the maximum number of simultaneously running daemons (max_count). Check if runnig_count <max_count - start the daemon, increment max_count by 1. Demon terminated - decrease max_count by 1

If we do through the file system: the flock function will help here. those. For each launch of the daemon, a lock file is created.

 class Lock { private static $_LockHandle; public static function lockfile($lockName) { self::$_LockHandle = fopen('/tmp/'.$lockName.'.lock', 'w+'); if (!flock(self::$_LockHandle, LOCK_EX | LOCK_NB)) { return False; } return True; } public static function unlock() { if(is_resource(self::$_LockHandle)) { fclose(self::$_LockHandle); } } } 

Well, the logic of work in the same way as with the base

 $lockName = 'DemonName'; $max = 10; $demonCounter = 0; $isDemonExecute = False; while ($demonCounter < $max) { $lockFileName = $lockName.$demonCounter; if (Lock::lockfile($lockFileName)) { // Делаем что нужно Lock::unlock($lockFileName) $isDemonExecute = True; break; } $demonCounter++; } if ($isDemonExecute) { // Сообщение о успешной работе } else { // Сообщение о нагрузке, попробуйте позже }