markevans / dragonfly

A Ruby gem for on-the-fly processing - suitable for image uploading in Rails, Sinatra and much more!
http://markevans.github.io/dragonfly
MIT License
2.12k stars 244 forks source link

Support for posix-spawn? #510

Open NARKOZ opened 4 years ago

NARKOZ commented 4 years ago

Would it be possible to use posix-spawn for shell commands?

It's more efficient than popen3 and provides meaningful process hierarchies because it performs a single fork/exec. (popen3 double forks to avoid needing to collect the exit status and also calls Process::detach which creates a Ruby Thread!!!!).

It handles all max pipe buffer (PIPE_BUF) hang cases when reading and writing semi-large amounts of data. This is non-trivial to implement correctly and must be accounted for with popen3, spawn, or hand rolled fork/exec code.

It's more portable than hand rolled pipe, fork, exec code because fork(2) and exec aren't available on all platforms. In those cases, POSIX::Spawn::Child falls back to using whatever janky substitutes the platform provides.

markevans commented 4 years ago

Hi - looks interesting, though I'd be reluctant to because I want to keep dependencies to a minimum. Was there a specific case where it made a big difference or where popen3 was being problematic? I don't mind popen3 being slightly less efficient if it's not the bottleneck

NARKOZ commented 4 years ago

Another popular image processing library - minimagick, has a plug-in support for it, the dependency being optional. Difference can be huge when dealing with thousands of images.