DWilliams Posted January 24, 2010 Share Posted January 24, 2010 I'm writing a CLI script that serves as a chat server. Most of it is coming together quite nicely now, but I'd like to have a chunk of code executed every 30 seconds to perform some cleanup tasks like removing unused chat rooms, writing user databases to file, and so on. As I understand it, PHP does not have support for multi-threading. Is there any way to do this without blocking the execution of the program? My program has one main loop that loops every time something gets sent to it via socket, so I could do something like check a "last executed" timestamp to see if it's been 30 seconds since the last timed execution and then run my code based on that. The problem with this approach is that it's neither accurate nor reliable, if the server goes for a long while without being sent anything, the commands will not be executed. Is there any other way to do this? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 24, 2010 Share Posted January 24, 2010 If on UNIX / LINUX look into a CRON job http://en.wikipedia.org/wiki/Cron Quote Link to comment Share on other sites More sharing options...
DWilliams Posted January 24, 2010 Author Share Posted January 24, 2010 If on UNIX / LINUX look into a CRON job http://en.wikipedia.org/wiki/Cron Yeah I'm running Linux but unfortunately I'm designing this script to be distributed to other users. Doing this would make installing the script much more complex and rule out cross-platform capability (and realistically most of my users will be on Windows). Quote Link to comment Share on other sites More sharing options...
laffin Posted January 24, 2010 Share Posted January 24, 2010 You can run background apps in windows, however. There are tools similar to *nix cron on windows as well. But if you can avoid running a background app with an intrepretive language, do so. as an interpretive language, you need the scripting engine, and php is kinda large. anyways, when your talking about running a task at specific intervals or background tasks, you rule out a lot of os's, as each os may use something different. so you usually end up writing stub files to handle each one. Quote Link to comment Share on other sites More sharing options...
calmchess Posted January 24, 2010 Share Posted January 24, 2010 do you mind using a small bit of javascript? you can use a simple ajax script to call a php script on interval. Quote Link to comment Share on other sites More sharing options...
DWilliams Posted January 24, 2010 Author Share Posted January 24, 2010 do you mind using a small bit of javascript? you can use a simple ajax script to call a php script on interval. This is a CLI script, it will not ever be run from a web browser. I don't know of any way to use JS/Ajax from a CLI script. Quote Link to comment Share on other sites More sharing options...
calmchess Posted January 24, 2010 Share Posted January 24, 2010 oh right could you check the time stamp and tell php to call a function if the timestamp is older then the current stamp by 30 seconds then loop back up and ask again would be a recursive function? i'm not sure if recursion is allowed in php. Quote Link to comment Share on other sites More sharing options...
laffin Posted January 24, 2010 Share Posted January 24, 2010 is this, cli an app. that is meant to continually run? it runs until a user request to exit? Does this task you need done, required only when its running? Quote Link to comment Share on other sites More sharing options...
DWilliams Posted February 5, 2010 Author Share Posted February 5, 2010 oh right could you check the time stamp and tell php to call a function if the timestamp is older then the current stamp by 30 seconds then loop back up and ask again would be a recursive function? i'm not sure if recursion is allowed in php. I believe what you are suggesting is the idea I initially had. The way my script is designed is that it loops forever, and upon each new loop it hangs until it sees socket activity, in which case it processes that activity then finishes the loop, starts another iteration, and hangs until something else happens. The problem the whole timestamp method is that overnight it could very easily happen that no input is received for like 6 hours or longer, so all these events would have to go unexecuted until somebody connects at which point they would fire at once. This is acceptable, just not ideal. It's looking like this is the route I will go unless somebody has other suggestions. is this, cli an app. that is meant to continually run? it runs until a user request to exit? Does this task you need done, required only when its running? Yes, it is a CLI app. It is a "chat server" of sorts so it will always be running and just sits there waiting for socket connections when nothing is going on. It will run pretty much continuously unless specifically shut down (I have shutdown procedures implemented but really it will always be running). I have several different tasks that need done and yes they have to be done while the program is running. Quote Link to comment Share on other sites More sharing options...
Omirion Posted February 6, 2010 Share Posted February 6, 2010 Why don't you just code a bot that opens a socket connections every 30 seconds. Quote Link to comment Share on other sites More sharing options...
Omirion Posted February 6, 2010 Share Posted February 6, 2010 Sorry for double post. Look up autoit. It has the things you need. http://www.autoitscript.com/autoit3/downloads.shtml I can help you with the coding actually. Or teach you how to make one. PM me if you're interested. Quote Link to comment Share on other sites More sharing options...
bspace Posted February 6, 2010 Share Posted February 6, 2010 Sorry for double post. Look up autoit. It has the things you need. http://www.autoitscript.com/autoit3/downloads.shtml I can help you with the coding actually. Or teach you how to make one. PM me if you're interested. the OP is running linux (see post 3) how is a windows ap going to help? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.