-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SplFileObject in spatie/fork #14587
Comments
SplFileObject is not designed to work in a parallel environment. And there is nothing that can be done about it. You cannot just start multithreading stuff without careful considerations. |
I am careful, just use it for read operations, should there be a note in the documentation for best practices, @Girgias also do you know a safe way todo this in php ? |
You are sharing an object which holds a pointer to a FILE across multiple threads, even for reading this is not parallel safe. You don't have two independent pointers that can move at their own pace. "Reading" something does not make it automatically safe for parallelization, see this SO response: https://stackoverflow.com/a/25411265 If you want to do parallel processing of the content of a file, you need to load the content of it in memory, and split it up into chunks to dispatch to the various threads. |
Hez @remco-pc 👋 a quick look reveals that After a I see two options for you moving forward:
Hope this helps. EDIT: Just an FYI: this is not a PHP thing, but just how operating systems behave in case of a fork, so you'd have the same problem when doing the same thing in C or Rust |
No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you. |
Description
Doing the following:
opening filepointers with splFileObject in the main thread and then using them multithreaded with the spatie/fork package in a child process causes
current()
to return an empty string after seek. this error leads to have some records missing in my framework r3m_io/framework with package r3m_io/node.I have moved the initialization of the filepointers to the child process and now i don't have missing records.
probably because it used to seek simultaniously to a different line on the same object
should splFIleObject be used simultaniously used, so initiate once, run in childprocess parallel
PHP Version
8.3.6
Operating System
Debian 12
The text was updated successfully, but these errors were encountered: