amphp / parallel-functions

Simplified parallel processing for PHP based on Amp.
https://amphp.org/parallel-functions
MIT License
271 stars 18 forks source link

How is it possible to parallelize file_get_contents? #4

Closed Bilge closed 6 years ago

Bilge commented 6 years ago

Sorry for posting a question in your issue tracker, but I don't understand how the following is possible.

$responses = wait(parallelMap([
    'https://google.com/',
    'https://github.com/',
    'https://stackoverflow.com/',
], function ($url) {
    return file_get_contents($url);
}));

I was told, in no uncertain terms, that such a thing would be impossible using async because file_get_contents() is, and always will be, a synchronous blocking operation. That is why Artax was created. If this is no longer the case then this is a breakthrough, but if not, and this library is actually using threads instead of coroutines, I think this should be clearly stated in the readme to avoid confusion.

joelwurtz commented 6 years ago

Internally, it create php processes (or threads depend on your php configuration) that execute this function, which have been serialized. I think the documentation is more clear about this here : https://amphp.org/parallel-functions/

Bilge commented 6 years ago

Oh I see, I thought Amp was all about coroutines, but nevertheless that is very clever and may indeed be some kind of breakthrough. 👍

kelunik commented 6 years ago

@Bilge Don't worry about questions in the issue tracker. Amp is mainly focused on non-blocking I/O, correct, but it can also do multi-processing. Consumers don't have to care, they get a promise either way. We also already used threading via libuv for filesystem access before.

Care to provide a PR to replicate the note from the docs into the README?

Bilge commented 6 years ago

Would love to, but if you observe my contribution history you may notice I'm a little busy at the moment.