Si basa sulla funzione flock di PHP, spero possa essere utile
https://github.com/jstar88/SafeIO

Codice: Seleziona tutto
crap0101@crap0101-virtual-machine:~/test$ php -a
Interactive shell
php > include("SafeIO/SafeIO.php");
php > $filePath = "data.txt";
php > SafeIO::startTransaction($filePath);
php > $contents = "some cool data";
php > $reset = true;
php > SafeIO::stopTransaction($filePath, $contents, $reset );
php > SafeIO::startTransaction($filePath);
PHP Warning: flock(): 3 is not a valid stream resource in /home/crap0101/test/SafeIO/SafeIO.php on line 77
PHP Warning: Uncaught exception 'Exception' with message 'Error while trying to get lock at data.txt' in /home/crap0101/test/SafeIO/SafeIO.php:77
Stack trace:
#0 php shell code(1): SafeIO::startTransaction('data.txt')
#1 {main}
thrown in /home/crap0101/test/SafeIO/SafeIO.php on line 77
Codice: Seleziona tutto
crap0101@crap0101-virtual-machine:~/test$ php -a
Interactive shell
php > include("SafeIO/SafeIO.php");
php > $filePath = "data.txt";
php > $contents = "some cool data";
php > $reset = true;
php > SafeIO::save($filePath, $contents , $reset);
php > SafeIO::startTransaction($filePath);
PHP Warning: flock(): 3 is not a valid stream resource in /home/crap0101/test/SafeIO/SafeIO.php on line 77
PHP Warning: Uncaught exception 'Exception' with message 'Error while trying to get lock at data.txt' in /home/crap0101/test/SafeIO/SafeIO.php:77
Stack trace:
#0 php shell code(1): SafeIO::startTransaction('data.txt')
#1 {main}
thrown in /home/crap0101/test/SafeIO/SafeIO.php on line 77
php >
chi sono i processi e utenti? quelli "normali" del SO? perchè io la intenderei così, ma non è quello che succedeTransactions are usefull to ensure an atomic behavior. Expecially, in generic applications , you need to execute actions that require time (like DB queries,loop etc) to know exactly what you need to do: in this situation a common problem is to keep unchanged the target file from modification of others processes.
include("../SafeIO.php");
SafeIO::startTransaction("data.txt","hello world!");
//<----
// ... Other users can't write data.txt while you are inside here ...
//<----
SafeIO::stopTransaction("data.txt"); // now the lock is released
echo SafeIO::open("data.txt");
Codice: Seleziona tutto
crap0101@crap0101-virtual-machine:~/test$ php -a
Interactive shell
php > include("SafeIO/SafeIO.php");
php > SafeIO::startTransaction("data.txt","hello world!");
php > // modifico il file da un altro processo: echo 222 > data.txt
php > SafeIO::stopTransaction("data.txt");
php > echo SafeIO::open("data.txt");
222
Codice: Seleziona tutto
php > include("SafeIO/SafeIO.php");
php > SafeIO::startTransaction("data.txt","hello world!");
php > $f = fopen("data.txt", "a+");
php > fwrite($f, "XXX");
php > SafeIO::stopTransaction("data.txt");
php > echo SafeIO::open("data.txt");
XXX
PHP istanzia un processo univoco per ogni request, a differenza di altri linguaggi come il Java.Infatti un limite prestazionale è la saturazione della CPU anche se non del tutto utilizzata.non capisco una cosa: se flock() non supporta il multithreading, quante probabilità ci sono di agire sullo stesso file contemporaneamente di cui l'autore del codice non è a conoscenza?
fixatoAltra cosa - non so se è un comportamento "previsto" - è normale non poter riutilizzare lo stesso path durante una sessione?
quì hai commesso 2 errori: il primo è quello di utilizzare funzioni esterne, che rilasciano il lock in automatico(come il manuale php dice).php > include("SafeIO/SafeIO.php");
php > SafeIO::startTransaction("data.txt","hello world!");
php > $f = fopen("data.txt", "a+");
php > fwrite($f, "XXX");
php > SafeIO::stopTransaction("data.txt");
php > echo SafeIO::open("data.txt");
XXX
Codice: Seleziona tutto
$safeFile = new ConcurrentFile( 'file.ext' );
$safeFile->write('something to write');
$safeFile->close();Codice: Seleziona tutto
$safeFile = new ConcurrentFile( 'file.ext' );
$safeFile->write('something to write');
$safeFile->close();Codice: Seleziona tutto
$safeFile = new ConcurrentFile( 'file.ext' );
$data = $safeFile->read();
$safeFile->close();Codice: Seleziona tutto
$safeFile = new ConcurrentFile( 'file.ext' );
$safeFile->writelock();
//Scrivi quello che ti pare nel tempo che vuoi
$safeFile->releasewritelock();
$safeFile->close();
Riguardo la fwrite, sicuro? forse in versioni vecchie, nella doc vedo degli esempi in cui flock() è usato proprio insieme a fwrite.disko [url=http://forum.ubuntu-it.org/viewtopic.php?p=4429725#p4429725][img]http://forum.ubuntu-it.org/images/icons/icona-cita.gif[/img][/url] ha scritto:quì hai commesso 2 errori: il primo è quello di utilizzare funzioni esterne, che rilasciano il lock in automatico(come il manuale php dice).php > include("SafeIO/SafeIO.php");
php > SafeIO::startTransaction("data.txt","hello world!");
php > $f = fopen("data.txt", "a+");
php > fwrite($f, "XXX");
php > SafeIO::stopTransaction("data.txt");
php > echo SafeIO::open("data.txt");
XXX
Il secondo è un errore logico,stai scrivendo dallo stesso processo.
Intendo che la funzione la puoi usare ma devi essere con un handle lokkato, se lo cambi non funziona più .PHP supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work)
Visualizzano questa sezione: steff e 5 ospiti