[PHP] scope and return array( ); problems
hi all, I have this function: function preg_array($pattern, $array, $r_array) { global $match; global $r_array; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $GA=array_slice($match,1); break; } } return array ($GA, $match); }
[PHP] scope and return array( ); problems
Hi all, I have this code ( but you already know it ;-) : function preg_array($pattern, $array, $r_array) { global $match; global $r_array; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $r_array=array_slice($match,1); break; } } return array ($r_array, $match); } $GA = array(); preg_array(/^GA\s+(\d+.\d+)\s+(\d+.\d+)/i, $PF, $GA) but the array $GA is empty outside the function, i tried to declare $GA also as global inside the function and it didn't change the output. It works if the function is change to this : function preg_array($pattern, $array, $GA) { global $match; global $GA; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $GA=array_slice($match,1); break; } } return array ($GA, $match); } What's wrong with the first version of the preg_array function? do I really need to have the same array name in order to retrieve the third argument of the function? Thanks for your help, yvan
[PHP] Re: scope and return array( ); problems
On 4/18/07, Yvan Strahm [EMAIL PROTECTED] wrote: Hi all, I have this code ( but you already know it ;-) : function preg_array($pattern, $array, $r_array) { global $match; global $r_array; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $r_array=array_slice($match,1); break; } } return array ($r_array, $match); } $GA = array(); preg_array(/^GA\s+(\d+.\d+)\s+(\d+.\d+)/i, $PF, $GA) but the array $GA is empty outside the function, i tried to declare $GA also as global inside the function and it didn't change the output. It works if the function is change to this : function preg_array($pattern, $array, $GA) { global $match; global $GA; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $GA=array_slice($match,1); break; } } return array ($GA, $match); } What's wrong with the first version of the preg_array function? do I really need to have the same array name in order to retrieve the third argument of the function? Thanks for your help, yvan sorry for the noise, a solution has been found : function preg_array($pattern, $array, $r_array) cheers yvan
Re: [PHP] crontab, PHP and MACOSX
On 3/28/07, Chris [EMAIL PROTECTED] wrote: There is a lot of require_once(' ') in the script and if i am not in the correct folder the script won't work. That is something php related ;) Change your require to something like this: require(dirname(__FILE__) . '/other_file_name.php'); -- Postgresql php tutorials http://www.designmagick.com/ Thanks for the hint. I found s solution to execute the php script from the cron scheduler: add a at the end of the crontab command Thank for your help. yvan
Re: [PHP] usage of flock
On 3/23/07, Richard Lynch [EMAIL PROTECTED] wrote: On Fri, March 23, 2007 7:52 pm, Yvan Strahm wrote: I am confused with the flock function and its usage. I have jobs which are stored in a database, these jobs are run by a series of job_runners scripts but sometimes the job_runners stop ( server or php crash-down). So i put a job_controller in crontab to check regularly if the runners run. But after a while I have a bunch of job_controller running, so to avoid that I tried to use flock. I try to put this in the job_controller: $wouldblock=1; $f=fopen(controller.lock, r); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die(Error! cant lock!); hoping that as long as the first job_controller run or don't close the file handle, a second job_controller won't be able to lock the controller.lockfile and die, but it didn't work. I also try this: $wouldblock=1; $f=fopen(controller.php, r); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die(Error! cant lock!); hoping the first job_controller will lock it-self, but it didn't work. I also thought of writing in the lock file the PID of the first job_controller and then compare it and if it doesn't match then die, but my main concern is , if the server crash down the surviving lock file will prevent any job_controller to start. So how could prevent multiple instance of the same script? Is flock the best way? You can do it with flock, but then you end up sooner or later with a locked file from an exit or killed script, and then you have to know to remove locks older than X minutes. You could also just do a mkdir for your lock, and check its filemtime. You could even use touch within loop of the script to make sure the script is still going, and safely assume that any lock older than X seconds is stale and can be ignored/removed. A final option is to use 'exec' to figure out if another process is running already: //bail out if it's already running: $pid = getmypid(); $command = /bin/ps aux | grep . __FILE__ . | grep -v grep ; exec($command, $existing, $error); if ($error) die(OS Error: $error\n . implode(\n, $existing) . \n); $other_count = 0; foreach($existing as $procline){ if (!strstr($procline, $pid )) $other_count++; } if ($other_count) exit; This allows you to be sure there is always one, and only one, running prcess or this file, with no assumptions about lock files maybe being stale. I use different ones at different times, depending on what the process needs to do, and how critical it is that it runs frequently. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? Thanks very much for the code, it works nicely. Just have to adjust the command, __FILE__ returns the absolute path to the script but ps returns only the relative path. So the existing array was empty. Thanks again yvan
Re: [PHP] usage of flock
On 3/23/07, Myron Turner [EMAIL PROTECTED] wrote: Richard Lynch wrote: On Fri, March 23, 2007 7:52 pm, Yvan Strahm wrote: I am confused with the flock function and its usage. I have jobs which are stored in a database, these jobs are run by a series of job_runners scripts but sometimes the job_runners stop ( server or php crash-down). So i put a job_controller in crontab to check regularly if the runners run. But after a while I have a bunch of job_controller running, so to avoid that I tried to use flock. I try to put this in the job_controller: $wouldblock=1; $f=fopen(controller.lock, r); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die(Error! cant lock!); hoping that as long as the first job_controller run or don't close the file handle, a second job_controller won't be able to lock the controller.lockfile and die, but it didn't work. I also try this: $wouldblock=1; $f=fopen(controller.php, r); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die(Error! cant lock!); hoping the first job_controller will lock it-self, but it didn't work. I also thought of writing in the lock file the PID of the first job_controller and then compare it and if it doesn't match then die, but my main concern is , if the server crash down the surviving lock file will prevent any job_controller to start. So how could prevent multiple instance of the same script? Is flock the best way? You can do it with flock, but then you end up sooner or later with a locked file from an exit or killed script, and then you have to know to remove locks older than X minutes. You could also just do a mkdir for your lock, and check its filemtime. You could even use touch within loop of the script to make sure the script is still going, and safely assume that any lock older than X seconds is stale and can be ignored/removed. I've never used locks in PHP, but have used them in Perl. In Perl a lock is automatically released on exit or when the locked file is closed. Is that not the same in PHP? According the the man page for the C version of flock, it too releases the lock on close and C's exit closes all streams. So, Perl is consistent with that. Just wondering for myself it this isn't the case with PHP, in case I ever want to use a lock. It's not clear to me from the original question, Yvan, whether you are able to get any lock at all. That is, if you are using LOCK_NB and a lock is already on the locked file, then the lock will be refused and the call will return immediately without a lock. So, the only way to use LOCK_NB is in a loop. Generally, this is not recommended, because in the time the loop gets back to making a second call, another file might have gotten the lock. However, in your case this might not matter, since you are the only one sending out these job controllers, and eventually all your processes will have had a chance at the lock file. On the other hand, if you use LOCK_EX, the call will block until a lock is available. Again, in your case this might not matter either. The danger here is that you will get a process that doesn't shut down. You can deal with this by setting an alarm which is caught using a signal handler that can exit the process. If you use LOCK_NB with a loop, you can also break out of the loop and exit after a time as well. You'd probably want to use sleep to time your loop, not a counter, since the counter might swallow up cpu. -- _ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/ The controller script never get lock. I was using the LOCK_NP in a infinite loop without success. Richard's code works well so I guess i will not use flock in this particular case. Thanks for your answer Cheers yvan
[PHP] usage of flock
Hello All, I am confused with the flock function and its usage. I have jobs which are stored in a database, these jobs are run by a series of job_runners scripts but sometimes the job_runners stop ( server or php crash-down). So i put a job_controller in crontab to check regularly if the runners run. But after a while I have a bunch of job_controller running, so to avoid that I tried to use flock. I try to put this in the job_controller: $wouldblock=1; $f=fopen(controller.lock, r); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die(Error! cant lock!); hoping that as long as the first job_controller run or don't close the file handle, a second job_controller won't be able to lock the controller.lockfile and die, but it didn't work. I also try this: $wouldblock=1; $f=fopen(controller.php, r); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die(Error! cant lock!); hoping the first job_controller will lock it-self, but it didn't work. I also thought of writing in the lock file the PID of the first job_controller and then compare it and if it doesn't match then die, but my main concern is , if the server crash down the surviving lock file will prevent any job_controller to start. So how could prevent multiple instance of the same script? Is flock the best way? Thanks in advance for your time and help cheers yvan