process any amount of data. any time - symfony live

47
Process any amount of data. Any time Juozas Kaziukėnas // juokaz.com // @juokaz

Upload: juozas-kaziukenas

Post on 18-Nov-2014

1.473 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Process any amount of data. Any time - Symfony Live

Process any amount of data. Any time

Juozas Kaziukėnas // juokaz.com // @juokaz

Page 2: Process any amount of data. Any time - Symfony Live
Page 3: Process any amount of data. Any time - Symfony Live

Juozas Kaziukėnas, Lithuanian

You can call me Joe

More info http://juokaz.com

Page 4: Process any amount of data. Any time - Symfony Live

The problem

Page 5: Process any amount of data. Any time - Symfony Live

CRON

Page 6: Process any amount of data. Any time - Symfony Live

PHP developers are lazy

Page 7: Process any amount of data. Any time - Symfony Live
Page 8: Process any amount of data. Any time - Symfony Live

Code should be lazy

Page 9: Process any amount of data. Any time - Symfony Live

file_get_contens

Page 10: Process any amount of data. Any time - Symfony Live

Buffering

Page 11: Process any amount of data. Any time - Symfony Live

Wait

Page 12: Process any amount of data. Any time - Symfony Live

Memory usage

Page 13: Process any amount of data. Any time - Symfony Live

Saw graph

Page 14: Process any amount of data. Any time - Symfony Live

Memory vs performance

Page 15: Process any amount of data. Any time - Symfony Live

Reading files

Page 16: Process any amount of data. Any time - Symfony Live

Line-by-line reading

$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {

$line = fgets($file_handle);

echo $line;

}

fclose($file_handle);

Page 17: Process any amount of data. Any time - Symfony Live

Line-by-line processing

$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {

$line = fgets($file_handle);

process($line);

}

fclose($file_handle);

Page 18: Process any amount of data. Any time - Symfony Live

XML

Page 19: Process any amount of data. Any time - Symfony Live

Read XML files

Page 20: Process any amount of data. Any time - Symfony Live

$z = new XMLReader;$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> nodewhile ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the treewhile ($z->name === 'product'){ // either one should work //$node = new SimpleXMLElement($z->readOuterXML()); $node = simplexml_import_dom($doc->importNode($z->expand(), true));

// now you can use $node without going insane about parsing var_dump($node->element_1);

// go to next <product /> $z->next('product');}

Page 21: Process any amount of data. Any time - Symfony Live

$z = new XMLReader;$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> nodewhile ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the treewhile ($z->name === 'product'){ // either one should work //$node = new SimpleXMLElement($z->readOuterXML()); $node = simplexml_import_dom($doc->importNode($z->expand(), true));

// now you can use $node without going insane about parsing var_dump($node->element_1);

// go to next <product /> $z->next('product');}

Page 22: Process any amount of data. Any time - Symfony Live

Running processes

Page 23: Process any amount of data. Any time - Symfony Live

Pipes

Page 24: Process any amount of data. Any time - Symfony Live

Linux

wget http://example.com/lol.xml.gz

gunzip lol.xml.gz

Page 25: Process any amount of data. Any time - Symfony Live

Linux

wget -O- http://example.com/lol.xml.gz | gunzip

Page 26: Process any amount of data. Any time - Symfony Live

$command = "wget -O- $url | gunzip";

$process = proc_open($command, array( array("pipe","r"), array("pipe","w"), array("pipe","w") ),$pipes);

while(!feof($pipes[1])) { $buffer .= fgets($pipes[1], 128); if (strpos($buffer, '<Item>')) {

// detected start of the item } if (strpos($buffer, '</Item>')) {

// detected end of the item }}fclose($pipes[1]);proc_close($process);

Page 27: Process any amount of data. Any time - Symfony Live

Reading from MySQL

Page 28: Process any amount of data. Any time - Symfony Live

mysql_unbuffered_query

$lh = mysql_connect('server', 'uname', 'pword');

$qry = "SELECT * FROM my_bigass_table";

$rh = mysql_unbuffered_query($qry, $lh);

while ($res = mysql_fetch_row($rh))

{

process($res);

}

Page 29: Process any amount of data. Any time - Symfony Live

Outputting data

Page 30: Process any amount of data. Any time - Symfony Live

PHP

$result = '';

foreach ($data as $item) {

$line = 'Name ' . $item['name'] . PHP_EOL;

$result .= $line;

}

echo $result;

Page 31: Process any amount of data. Any time - Symfony Live

PHP

$result = '';

ob_end_clean();

foreach ($data as $item) {

echo 'Name ' . $item['name'] . PHP_EOL;

flush();

}

Page 32: Process any amount of data. Any time - Symfony Live

PHP 5.5

Page 33: Process any amount of data. Any time - Symfony Live

Generators

Page 34: Process any amount of data. Any time - Symfony Live
Page 35: Process any amount of data. Any time - Symfony Live

Yield

Page 36: Process any amount of data. Any time - Symfony Live

Line-by-line processing

$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {

$line = fgets($file_handle);

process($line);

}

fclose($file_handle);

Page 37: Process any amount of data. Any time - Symfony Live

Line-by-line processing

function read() { $file_handle = fopen("myfile", "r"); while (!feof($file_handle)) { $line = fgets($file_handle); yield $line; } fclose($file_handle);}

$data = read();foreach ($data as $line) {}

Page 38: Process any amount of data. Any time - Symfony Live

Making HTTP requests

Page 39: Process any amount of data. Any time - Symfony Live

Open connections limit

Page 41: Process any amount of data. Any time - Symfony Live

Next steps

Page 42: Process any amount of data. Any time - Symfony Live

Gearman

Page 43: Process any amount of data. Any time - Symfony Live

Hadoop

Page 44: Process any amount of data. Any time - Symfony Live

Why PHP?

Page 45: Process any amount of data. Any time - Symfony Live

What’s possible

Page 46: Process any amount of data. Any time - Symfony Live

Using it for good

Page 47: Process any amount of data. Any time - Symfony Live

THANKS!Juozas Kaziukėnas

@juokaz