Slide 1

Slide 1 text

¡Hola Madrid!

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Ceci n'est pas une pipe.

Slide 4

Slide 4 text

| Ceci n'est pas une pipe.

Slide 5

Slide 5 text

Understanding Unix pipes with Ruby Conferencia Rails 2016 Madrid, ES Sergio Gil iamserg.io @porras

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

ls -l

Slide 11

Slide 11 text

ls -l total 75096 -rw-r--r-- 1 sergio staff 235 Sep 29 00:30 -.8x -rw-r--r-- 1 sergio staff 235 Sep 29 00:31 -rwxr-xr-x -rw-r--r-- 1 sergio staff 1282 Sep 4 2015 BSDL -rw-r--r-- 1 sergio staff 203 Sep 4 2015 CONTRIBUTING.md -rw-r--r-- 1 sergio staff 2502 Sep 29 00:29 COPYING -rw-r--r-- 1 sergio staff 2624 Sep 4 2015 COPYING.ja -rw-r--r-- 1 sergio staff 312287 Oct 11 08:05 ChangeLog -rw-r--r-- 1 sergio staff 218 Sep 29 00:39 GNUmakefile -rw-r--r-- 1 sergio staff 18092 Sep 4 2015 GPL -rw-r--r-- 1 sergio staff 196 Sep 29 00:29 KNOWNBUGS.rb -rw-r--r-- 1 sergio staff 29083 Sep 29 00:29 LEGAL -rw-r--r-- 1 sergio staff 17402 Sep 29 00:39 Makefile -rw-r--r-- 1 sergio staff 16520 Sep 29 00:29 Makefile.in -rw-r--r-- 1 sergio staff 7196 Oct 11 08:05 NEWS -rw-r--r-- 1 sergio staff 28 Sep 4 2015 README.EXT -rw-r--r-- 1 sergio staff 43 Sep 4 2015 README.EXT.ja

Slide 12

Slide 12 text

ls -l | grep ^-........x -rwxr-xr-x 1 sergio staff 33271 Sep 29 00:39 config.status -rwxr-xr-x 1 sergio staff 731437 Sep 29 00:33 configure -rwxr-xr-x 1 sergio staff 3166548 Sep 29 00:40 miniruby -rwxr-xr-x 1 sergio staff 3164796 Sep 29 00:45 ruby

Slide 13

Slide 13 text

ls -l | grep ^-........x | wc -l 4

Slide 14

Slide 14 text

count(grep(ls(long: true), /^-.{8}x/))

Slide 15

Slide 15 text

files = ls(long: true) executables = grep(files, /^-.{8}x/) n = count(executables)

Slide 16

Slide 16 text

ls(long: true).grep(/^-.{8}x/).count

Slide 17

Slide 17 text

ls |> grep(/^-.{8}x/) |> count

Slide 18

Slide 18 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+

Slide 19

Slide 19 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ prog1 | prog2

Slide 20

Slide 20 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb

Slide 21

Slide 21 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb (1..5).each do |i| $stderr.puts "Sending #{i}" $stdout.puts i end $stderr.puts "Done sending"

Slide 22

Slide 22 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb (1..5).each do |i| $stderr.puts "Sending #{i}" $stdout.puts i end $stderr.puts "Done sending" $stdin.each do |line| puts "Received #{line}" end puts "Done receiving"

Slide 23

Slide 23 text

Sending 1 Sending 2 Sending 3 Sending 4 Sending 5 Done sending Received 1 Received 2 Received 3 Received 4 Received 5 Done receiving

Slide 24

Slide 24 text

Sending 1 Sending 2 Sending 3 Sending 4 Sending 5 Done sending Received 1 Received 2 Received 3 Received 4 Received 5 Done receiving Sending 1 Received 1 Sending 2 Received 2 Sending 3 Received 3 Sending 4 Received 4 Sending 5 Received 5 Done sending Done receiving

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

1. Results are streamed

Slide 39

Slide 39 text

tail -f log/development.log | grep POST

Slide 40

Slide 40 text

def ls(options = {}) Enumerator.new do |e| open("| ls #{'-l' if options[:long]}").each do |l| puts "Reading #{l.inspect}" e << l end end end

Slide 41

Slide 41 text

def grep(input, regex) Enumerator.new do |e| input.each do |l| puts "Filtering #{l.inspect}" e << l if l =~ regex end end end

Slide 42

Slide 42 text

def count(input) total = 0 input.each do |l| puts "Counting #{l.inspect}" total += 1 end total end

Slide 43

Slide 43 text

count(grep(ls(long: true), /^-.{8}x/))

Slide 44

Slide 44 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n"

Slide 45

Slide 45 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n"

Slide 46

Slide 46 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n"

Slide 47

Slide 47 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n"

Slide 48

Slide 48 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n"

Slide 49

Slide 49 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n"

Slide 50

Slide 50 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n"

Slide 51

Slide 51 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Filtering "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n"

Slide 52

Slide 52 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Filtering "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Reading "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n"

Slide 53

Slide 53 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Filtering "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Reading "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Filtering "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n"

Slide 54

Slide 54 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Filtering "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Reading "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Filtering "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Counting "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n"

Slide 55

Slide 55 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Filtering "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Reading "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Filtering "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Counting "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n"

Slide 56

Slide 56 text

count(grep(ls(long: true), /^-.{8}x/)) Reading "total 24\n" Filtering "total 24\n" Reading "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Filtering "-rw-r--r-- 1 sergio staff 260 Oct 7 00:34 1.rb\n" Reading "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Filtering "-rw-r--r-- 1 sergio staff 525 Oct 7 00:40 2.rb\n" Reading "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Filtering "-rw-r--r-- 1 sergio staff 0 Oct 7 00:22 3.rb\n" Reading "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Filtering "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" Counting "-rwxr-xr-x 1 sergio staff 22 Oct 7 00:26 dummmy\n" 1

Slide 57

Slide 57 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb

Slide 58

Slide 58 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb (1..5).each do |i| $stderr.puts "‘Calculating’ #{i}" sleep 1 $stderr.puts "Sending #{i}" $stdout.puts i end $stderr.puts "Done sending"

Slide 59

Slide 59 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb (1..5).each do |i| $stderr.puts "‘Calculating’ #{i}" sleep 1 $stderr.puts "Sending #{i}" $stdout.puts i end $stderr.puts "Done sending" $stdin.each do |line| puts "‘Processing’ #{line}" sleep 1 puts "Processed #{line}" end puts "Done receiving"

Slide 60

Slide 60 text

5 items

Slide 61

Slide 61 text

5 items 2 steps per item

Slide 62

Slide 62 text

5 items 2 steps per item 1 second per step

Slide 63

Slide 63 text

5 items 2 steps per item 1 second per step Total: 10 seconds

Slide 64

Slide 64 text

5 items 2 steps per item 1 second per step Total: 10 seconds 6 seconds

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

2. Execution is parallel

Slide 75

Slide 75 text

def ls(options = {}) output_queue = Queue.new Thread.new do open("| ls #{'-l' if options[:long]}").each do |l| output_queue << l end output_queue.close end output_queue end

Slide 76

Slide 76 text

def grep(input_queue, regex) output_queue = Queue.new Thread.new do while l = input_queue.pop output_queue << l if l =~ regex end output_queue.close end output_queue end

Slide 77

Slide 77 text

def count(input_queue) total = 0 output_queue = Queue.new Thread.new do while l = input_queue.pop total += 1 end output_queue << total output_queue.close end output_queue.pop end

Slide 78

Slide 78 text

def count(input_queue) total = 0 output_queue = Queue.new Thread.new do while l = input_queue.pop total += 1 end output_queue << total output_queue.close end output_queue.pop end count(grep(ls(long: true), /^-.{8}x/))

Slide 79

Slide 79 text

Concurrent

Slide 80

Slide 80 text

Concurrent Parallel

Slide 81

Slide 81 text

+-----------+ +-----------+ +-----------+ | | | | | | | 300/s +-----> 100/s +-----> 100/s | | | | | | | +-----------+ +-----------+ +-----------+

Slide 82

Slide 82 text

+-----------+ | | +--> 100/s +--+ | | | | | +-----------+ | +-----------+ | +-----------+ | +-----------+ | | | | | | | | | 300/s +--+--> 100/s +--+--> 300/s | | | | | | | | | +-----------+ | +-----------+ | +-----------+ | +-----------+ | | | | | +--> 100/s +--+ | | +-----------+

Slide 83

Slide 83 text

+-----------+ +-----------+ +-----------+ | | | | | | | 300/s +-----> 100/s +-----> 100/s | | | | | | | +-----------+ +-----------+ +-----------+

Slide 84

Slide 84 text

+-----------+ +-----------+ +-----------+ | | | | | | | 300/s +-----> 100/s +-----> 100/s | | | | | | | +-----------+ +-----------+ +-----------+

Slide 85

Slide 85 text

+-----------+ +-----------+ +-----------+ | | | | | | | 300/s +-----> 100/s +-----> 100/s | | | | | | | +-----------+ +-----------+ +-----------+ BACK PRESSURE

Slide 86

Slide 86 text

+-----------+ +-----------+ +-----------+ | | | | | | | 300/s +-----> 100/s +-----> 100/s | | | | | | | +-----------+ +-----------+ +-----------+ BACK PRESSURE 100

Slide 87

Slide 87 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb

Slide 88

Slide 88 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb (1..50000).each do |i| $stderr.puts "Sending #{i}" if i % 5000 == 0 $stdout.puts i end $stderr.puts "Done sending"

Slide 89

Slide 89 text

+---------+ |PROCESS 1| +---------+ err| |out | | | | | |in | +-v-------+ | |PROCESS 2| | +---------+ | err| |out | | | | | | | | | +-v-----v-----v-+ | TERMINAL | +---------------+ ruby prog1.rb | ruby prog2.rb (1..50000).each do |i| $stderr.puts "Sending #{i}" if i % 5000 == 0 $stdout.puts i end $stderr.puts "Done sending" $stdin.each do |line| sleep 1.0 / 5000 puts "Processed #{line}" if line.to_i % 5000 == 0 end puts "Done receiving"

Slide 90

Slide 90 text

Sending 5000 Sending 10000 Sending 15000 Processed 5000 Sending 20000 Processed 10000 Sending 25000 Processed 15000 Sending 30000 Processed 20000 Sending 35000 Processed 25000 Sending 40000 Processed 30000 Sending 45000 Processed 35000 Sending 50000 Done sending Processed 40000 Processed 45000 Processed 50000 Done receiving

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

3. Back pressure for free

Slide 93

Slide 93 text

SizedQueue.new(BUFFER_SIZE)

Slide 94

Slide 94 text

Concurrency

Slide 95

Slide 95 text

Concurrency Parallelism

Slide 96

Slide 96 text

Concurrency Parallelism Back Pressure

Slide 97

Slide 97 text

Pipes are awesome and literally the best!

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Dynamic Typing?

Slide 103

Slide 103 text

Dynamic Typing? Try with ‘Everything is a string’!

Slide 104

Slide 104 text

Error handling gets complicated

Slide 105

Slide 105 text

This is not a talk about pipes.

Slide 106

Slide 106 text

¡Gracias! Sergio Gil iamserg.io @porras