Sleep is an essential function if you want to delay execution for a process to wait while other process is computing some data.
Please bring it to life ![]()
Sleep is an essential function if you want to delay execution for a process to wait while other process is computing some data.
Please bring it to life ![]()
It’s impossible to adjust PHP settings for individual accounts.
Free hosting also doesn’t really want someone to have long running processes, so sleep is probably blocked on purpose and might never be enabled.
The sleep function is disabled because it hogs a PHP process while doing nothing. It just wastes server connections slots for no reason.
I’m not sure what you mean by this:
Can you give a specific example for this for something that you would actually do on our hosting?
Because this use case seems both far fetched, and using sleep to coordinate between processes should be a last resort in my opinion.
Maybe I’m missing something, but to do this, cant you just call one routine then the next? Or put everything in one script one step at a time?
Let’s imagine the f1() function that does a havy calculation but needs to do it once. And the f2 function that represents a regular workflow.
So let’s make the workflow() function that makes all other requests to wait for the first request to finish the calculation.
function workflow (string $flag, callable $doOnce, callable $doAlways, int $wait = 1)
{
$flag_always = $flag . '-always'; // the flag to tell us to execute $doAlways()
if (is_dir($flag_always)) $doAlways();
else
{
if (@mkdir($flag))
// a flag exists
// @ - suppresses errors
// returns 'true' if a dir is created, returns 'false' if a dir already exists
// (atomic operation)
{
$doOnce();
mkdir($flag_always);
rmdir($flag); //remove a flag
$doAlways();
}
else // no flag
{
sleep($wait);
workflow($flag, $doOnce, $doAlways, $wait); // check again
}
}
}
function f1 ()
{
cache_create('general'); // some heavy calculations
}
function f2 ()
{
cache_use('general'); // uses the result of heavy calculations
}
function main ()
{
workflow('main', f1(...), f2(...));
}
main();
It is the easiest way to add some states to your app from the basic php box.
I understand now. So the use case is a shared lock between requests to make sure that some expensive operation is not done twice, and you want to have all other requests block while waiting for the first process to complete.
While this is certainly a way to prevent concurrent execution of the heavy calculation, it’s generally speaking a not very efficient way to do it.
Having the PHP requests wait while the expensive operation is being done means that these requests are consuming PHP processes. This can quickly use up your concurrent process limit, and result in your website not being able to handle additional requests, or even being suspended for 24 hours.
Not to mention that having the browser just wait for a response is not great UX.
A better approach is to just return a message to the browser saying that the data is being generated. You can then just have the user refresh the page, or handle it with Javascript to poll the server to check if the data is ready.
I admit that not having the sleep() function will probably make this functionality a bit harder to implement for you right now. However, know that the alternative is more scalable and provides better UX, so it’s a better end result in the long term.
There are no alternatives besides prebuilding or special requests or operations to initialize the data from the outside.
Js has noting to do with it. As it is basically a prebuilding phase of an app which is turned into a special state in an cheap out of the box way.
It doesn’t take much time or processor resources but makes things easier.
I really think that all standard functions must be available. Sleep is basic and doesn’t hurt anyone. Disabling sleep brings more harm than good ![]()
You’re focusing on processor time, but there’s another critical resource: connection slots.
Every PHP process, even while sleeping, consumes an Entry Process slot. Our hosting accounts (like most other hosting providers) have a limit on concurrent Entry Processes. If you hit this limit, your website stops accepting new connections until processes complete. And (at least on our hosting) if that happens repeatedly, your account gets suspended for 24 hours.
So using sleep() to wait for cache generation directly risks getting your site suspended, especially if multiple users hit the uncached operation simultaneously. That’s why I’m suggesting the polling approach: it keeps your Entry Processes free and eliminates the suspension risk.
The sleep() function is disabled platform-wide specifically to prevent these kinds of resource exhaustion issues for accounts. Because even if you’re willing to accept this risk, we’re not willing to accept the risk to get blamed for unfair suspensions when it’s their code that’s responsible for it.
It is like prohibiting eating strawberries for all just because some people have allergy. You already have the policy. And that is ok. Let people decide for themselves.
In the scenario I am speaking about, the wait time is minimal (few seconds once) but it really makes things easier.
It is not a good thing when in order to restrain very rare malignant scenarios, you completely disable useful benevolent ones ![]()
It is what it is. The policies on Free hosting are there to protect everyone. So yes, sometimes a bad few ruin it for everyone. But that’s the way it goes.
If you really need it, you can check with IFastNet if premium hosting can use sleep. But that does cost money. But as you’d expect, a paid service is a lot less restricted than a free service
If you have enough concurrent traffic that cache stampede is a real concern, then having multiple requests sleep while waiting will exhaust your Entry Processes and get your account suspended.
If you don’t have that much traffic, then occasional concurrent cache generation isn’t actually a problem. The chance that there will be concurrent cache generation is very small, and if it does happen, then doing an operation of “a few seconds” twice is no problem.
Either way, the locking mechanism you’re trying to implement doesn’t make sense for your scenario.
I have already explained why this approach is against your own interest, and provided you with a practical alternative that avoids these issues. However, the sleep() function will remain disabled. If your application requires it, you’ll need to find a different hosting service that lets you use this function.