1

after reading this:

PHP Threads and Synchronization

is my assumption correct that even if I have a singelton pattern, there actually is 1 instance of this class per request? What about flock?

I need to limit a certain functionality/code block in my application to be only executed by one request at a time. The reason is a bit strange, I need to call a cmd-line tool that reads a txt file I create in php and then writes the result to several txt files. To prevent false/strange results the whole code block create file - run cmd-line- read result should be synchronized.

How could i do that? I tried it with a dummy file an lock it with flock -> does not work. I also tried a simple boolean field isLocked to stop further execution of a second request. Also does not work. The issue is the first request is processed correctly (data inserted into DB) but the second not and both request do not finish, page loads forever.

Ideas? solutions?

EDIT:

$lockFile = fopen("lock", "r");
while (!flock($lockFile, LOCK_EX)) {

    sleep(1000);
}
// do work
flock($lockFile, LOCK_UN);
fclose($lockFile);

Or:

if (myClass::$isLocked) {
  return false;
}
myClass::$isLocked = true
// do work
myClass::$isLocked = false

both versiosn don't work and both requests freeze, eg. next page never loads. I need to kill apache process.

Community
  • 1
  • 1
beginner_
  • 331
  • 1
  • 3
  • 14
  • can you use locking facilities of your database? (in mysql lock table, for example?) – keppla Apr 19 '11 at 06:59
  • At work we once resolved such a problem by locking a mysql table. Don't no the details as I was not the one who actually implemented it. – Eelke Apr 19 '11 at 07:04
  • I suppose I could use the database. How would I do that? or said otherwise how do i test if a table is locked? @Bogdan I'm a programming noob so I let the experts answer the questions. – beginner_ Apr 19 '11 at 07:49

1 Answers1

1

Yes, Apache serves the request and then moves on. This means unless you put something in the session, it will cease to exist at the end of the request.

If you want to lock files in you can use flock (as you've indicated) : http://php.net/manual/en/function.flock.php

And if you want to manage state in the DB, you can use transactions (mysql link) : http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html

JohnP
  • 49,507
  • 13
  • 108
  • 140
  • I'm still a little confused. Singelton pattern then has no use in php? Putting soemthing in the session deos not help, I would need to have globally only 1 instance of the class and use this same instance for all requests. – beginner_ Apr 19 '11 at 08:17
  • @beginner_ indeed, the singleton pattern has limited use in regards to PHP because it has no application scope. It'll just ensure that there is only 1 instance inside the request. You should use file locking or semaphors – JohnP Apr 19 '11 at 08:38
  • I tired file locking but it just does not work. It blocks the whole application. See code in edited first post. – beginner_ Apr 19 '11 at 10:33
  • typically, blocking is the desired behaviour (as in 'wait until your turn'), but, as stated in http://de3.php.net/manual/en/function.flock.php, you could use the flag LOCK_NB. (the quality of the documentation varies between languages, in the german variant, this flag is not that well explained, therefore i suggest to always read the english one) – keppla Apr 20 '11 at 06:31
  • I've tried a ton of different options, also one that just creates and deletes a file. If it is present -> lock if not go ahead and calculate. Doesn't work. Application blocks. What about a global variable? either put the object in it or just a boolean. Good or very bad practice? – beginner_ Apr 21 '11 at 09:10
  • @beginner_ global variables are global only for the request scope – JohnP Apr 21 '11 at 09:43