RubixML / ML

A high-level machine learning and deep learning library for the PHP language.
https://rubixml.com
MIT License
2.03k stars 182 forks source link

Saving models to PHAR file results in error #340

Open nviet opened 3 months ago

nviet commented 3 months ago

Hi there. First of all I'd like to thank you for this library as I have been playing with it to learn Machine Learning for quite some time.

So today I wanted to save the models to a PHAR file, I tried this:

$persister = new Filesystem("phar://model.phar/model.rbx", false);
$serializer = new RBX(6);
$encoding = $serializer->serialize($persistable);
$persister->save($encoding);

Then I got the Exception message Could not write to the filesystem. The reason is because of this line in the file Persisters/Filesystem.php:

$success = file_put_contents($this->path, $encoding->data(), LOCK_EX);

The flag LOCK_EX can only be used if the file is within local filesystem (the file wrapper must start with file://). Other than that, PHP will throw a warning message Exclusive locks may only be set for regular files as defined in its source code file.c.

So if anyone tries to save the models to other file wrappers such as http:// or ftp://, it will not success too.

andrewdalpino commented 3 months ago

Hey @nviet thanks for the great report!

The reason for the LOCK_EX i.e. exclusive lock is to prevent another process from writing to the file at the same time a model is being saved for example. This is an edge case for many people, but may come into play for some.

What if we made locking an argument of the constructor instead of hardcoding it to on? Would that solve the problem in your case?