• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

setting a timelimit to functions?

Joined
Jun 26, 2006
Messages
37
i'm looking for a way to set a function to time out, meening that if it isn't able to complete itself within a given period of time, then it aborts the function.

the reason i need something like this is, i programmed a forum and i have it check the image sizes while i process the bbc, which works nice to scale down images that people aren't nice enough to scale down a little. well i've noticed that when people have their images hosted on slower server then it takes a while to check the image size which makes the post take a while to load, so while my page looks like it's wanting to time out, it's just taking a while on someone's slow images.

so i want to know if theres a way that i can call a function and if it doesn't complete in a certain amount of time then i can abort it? - thus just leaving the image at whatever its default size is

btw, this is being done in php, prolly shouldda mentioned that earlier
 
Are you closing a socket connection? If not try that before extending a timeout as it may be unnecessary. PHP has something like set_time_limit() or something similar that allows longer waiting periods. You may also want to resort to your ini config files to expand the timeout in apache and/or php
 
thanks for the reply i was worried no one had any answers

the host is on a shared host and i dont have access to the ini file :(

this is close but after looking more at it, it looks like almost the opposite of what i need. this is why i'm not sure if it's even possible. i would like to do something like...


PHP:
while($time < $xseconds){
 run some script, if it doesn't execute in time, then to bad for that script
}

now i know that'll run over and over until the condition is met, but i just want to set a timelimit on a single script execution, and thats the best way i could think of expressing it, pretty poor example i know :(


instead of allowing scripts to go for a longer duration, i want to cut them short if they start taking to long.
 
Cancellable asynchronous execution and timeouts certainly exist, sure. How you'll implement them depends a lot on your operating environment: what OS you're using, what language you're using, and what the cancellable activity does.

I don't think PHP offers enough control over threads or processes to do what you want in the way that your previous post suggests -- that is, that you'll terminate another script.

On the other hand, it might be possible for you to modify the worker script to quit after a certain amount of time. That would actually happen in a way that's not different than the code fragment you post. If the inner script has a loop that does work:

Code:
While more things to process
   get the next thing to process
   process it
   write results
wend


you can modify it to quit after a certain period of time:

Code:
while more things to process and time hasn't run out
   get the next thing to process
   process it
   write results
wend

Such an approach can be simple an effective. If the time to process each work unit is regular and granular, this is probably the way to go. If it's irregular or very large anyway, you might need a little more logic -- or you might want to find a way to break apart the larger tasks and make them continuable or restartable.
 
The limiting factor is going to be launching PHP with Apache. A web based PHP script has a little less functionality on this end then a shell based PHP script.

Anyway, one solution might be to launch another script (using exec(), system(), or shell_exec()) that handles this, maybe in a language a little happier to set constraints on its self.


Edit: Well, it appears I almost completey misread how you were doing things :) Edited to make more 'sense'.
 
Back
Top