SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
Tehnici Php: Manipularea Proceselor April 25, 2009




                            Tehnici Php:
                Manipularea proceselor

Tudor Popa
Web2.0 Jedi
Server + Client Side Efficiency Yoda Wannabe
Semantic Web Jedi
Wixi.com Jedi

                                                   Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 1
Tehnici Php: Manipularea Proceselor April 25, 2009


Lucrul cu procese in Php(contra):




  “Cam greu de implementat pe WAMP!”
  “De ce sa ma complic cu procese cand pot face totul din acelasi script?!”
  “Exista riscul folosirii excesive a memoriei din cauza aparitiei mai multor instante!!!”
  “Cum pot sti daca ruleaza sau nu?!”
  “Mai bine ma folosesc de CRON!”
  “Daca ruleaza la nesfarsit si nu il pot stapani??”
  “Credeam ca am scapat de procese cand am terminat-o cu C-ul!”
  “Procese?! Ba, credeam ca suntem la GeekMeet, nu la tribunal…”



                                                        Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 2
Tehnici Php: Manipularea Proceselor April 25, 2009


Modalitati de abordare

PCNTL:

Respecta stilul Unix in:
 Crearea proceselor
 Executia programului
 Manipularea semnalelor(cu ajutorul “ticks”-urilor)
 Terminarea proceselor
 Semantica




                                                        Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 3
Tehnici Php: Manipularea Proceselor April 25, 2009


Mic exemplu cu fork

<?php
    // Facem fork
    $pid = pcntl_fork();
    if ($pid == -1) {
        die(quot;Eroare la fork()quot;);
    } else if ($pid) {
        // Suntem in Parinte
        print “Parintele aici, PID-ul fiului este: $pidn”;
    } else {
        // Suntem in Fiu
       print “Fiul aici!n”;
    }




                                                                 Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 4
Tehnici Php: Manipularea Proceselor April 25, 2009


Mic exemplu cu tratarea semnalelor

<?php                                                               case SIGTERM:
    declare(ticks=1);                                                 // Semnal de shutdown
    // Atribuim handlerul de semnale                                  exit;
    pcntl_signal(SIGTERM, quot;sig_handlerquot;);                             break;
    pcntl_signal(SIGHUP, quot;sig_handlerquot;);                            case SIGHUP:
    // Procesul propriu-zis                                           // Semnal de restart
    while (1) {                                                       break;
       // Executa ceva                                              default:
    }                                                                 // Alte semnale
    // Handlerul de semnale                                     }
    function sig_handler($signo) {                          }
        switch ($signo) {




                                                         Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 5
Tehnici Php: Manipularea Proceselor April 25, 2009




Rulare “manuala” prin Shell sau CRON:



     Administrare la nivel de consola
     Posibilitate de “supraveghere” (top, htop, supervise, etc.)
     Avantajul executarii unui singur task
     Avantajul rularii continue(daca se foloseste rularea prin CRON)
     Easy to strace




                                                          Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 6
Tehnici Php: Manipularea Proceselor April 25, 2009


Mic exemplu de proces pornit din Shell

processTest.php
<?php
  $evt = new UserEventsHandler();
  while(1) {
    if ($evt->checkDb()) {
       $evt->handleEvents();
       sleep(2);
    }
    else {
       exit();
    }
  }

Consola:
Cd <path_to_file>; php5 processTest.php
                                                       Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 7
Tehnici Php: Manipularea Proceselor April 25, 2009


Probleme legate de exemplul anterior
   Posibilitatea aparitiei mai multor instante(in cazul rularii prin CRON)
Rezolvare:
// Verificarea pid-ului
function checkPid($pid) {
   $cmd = quot;ps -o stat --no-headers $pidquot;;
   exec($cmd, $output, $result);
   if(count($output) >= 1){
       return true;
   }
   return false;
}
Output consola
$ ps -o stat --no-headers 6730
S+



                                                          Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 8
Tehnici Php: Manipularea Proceselor April 25, 2009


Folosing functia anterioara, verificam pid-ul salvat intr-un fisier

function checkPidFromFile() {                                        return false;
  if (file_exists(PATH_PID)) {                                   }
     $fh = fopen(PATH_PID, 'r');                              }
     $data = fgets($fh);                                      else {
                                                                return true;
    if ($data != null) {                                      }
       $pid = intval($data);                               }
       if (checkPid($pid)) {                               else {
          fclose($fh);                                       return false;
          return true;                                     }
       }                                               }
       else {




                                                           Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 9
Tehnici Php: Manipularea Proceselor April 25, 2009


Functia care scrie pid-ul curent intr-un fisier


function writePid() {
  $pid = getmypid();
  $fh = fopen(PATH_ PID, 'w+');
  fwrite($fh, strval($pid));
  fclose($fh);
}




                                                           Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 10
Tehnici Php: Manipularea Proceselor April 25, 2009


   Lipsa comunicarii(no output, “Silence is golden”, dar nu tot timpul)

Rezolvare:

function talk($pid) {
  if (isset($_GET['status'])) {
     // Afisam date despre proces
     ProcessHandler::tellProcessStatus($pid);
     die();
  }
}




                                                             Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 11
Tehnici Php: Manipularea Proceselor April 25, 2009


Varianta imbunatatita
// Verificam pid-ul din fisier
talk($pid);
if (!checkPidFromFile()) {
   // In cazul in care nu exista/nu ruleaza, il scriem
   writePid();
   $evt = new UserEventsHandler();
   while(1) {
      if ($evt->checkDb()) {
         $evt->handleEvents();
         sleep(2);
      }
      else {
         exit();
      }
   }
}

                                                                 Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 12
Tehnici Php: Manipularea Proceselor April 25, 2009


Cazuri favorabile utilizarii proceselor
  Data caching(exemplu Memcached)




                                                  Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 13
Tehnici Php: Manipularea Proceselor April 25, 2009




Scenariu:

  1. Userul cere date de la data controller
  2. Data controller-ul cauta in cache
       a. Daca rezultatele exista, le returneaza
       b. Daca nu, le ia din baza de date (,iar apoi le introduce in cache) si le returneaza
  3. Scriptul refreshData_p.php, rulat ca proces prin CRON, introduce continuu in cache date
     din baza de date




                                                        Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 14
Tehnici Php: Manipularea Proceselor April 25, 2009


Exemplu pentru modelul anterior:
refreshData_p.php
<?php
// Folosim functiile pt verificarea pid-ului si              CacheHandler::addToCache(QueryChecker::retur
talk                                                         nNewQueryData);
                                                                   sleep(2);
require_once(“processFunctions.php”);
                                                                 }
talk($pid);
                                                                 else {
// Verificam pid-ul din fisier                                     exit();
if (!checkPidFromFile()) {                                       }
   // In cazul in care nu exista/nu ruleaza, il scriem         }
   writePid();                                               }
   while(1) {
      if (QueryChecker::isNewDataInQueries()) {




                                                                 Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 15
Tehnici Php: Manipularea Proceselor April 25, 2009




Beneficii:

   Mentinerea continua a datelor noi in cache
   Izolare in cazul erorilor de server/umane
   Update la interval de timp scazut datorita executarii unui singur task




                                                         Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 16
Tehnici Php: Manipularea Proceselor April 25, 2009


 External server calls(exemplu community im)




                                                    Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 17
Tehnici Php: Manipularea Proceselor April 25, 2009


Scenariu:

  1. Userul se logheaza in site
  2. Executa evenimente:
       a. Schimba avatar-ul
       b. Schimba nickname-ul
       c. Schimba informatii personale
       d. Adauga prieteni
       e. Sterge prieteni
       f. Log-out
  3. Serverul trimite evenimentele user-ului la serverul de IM si is continua taskurile
  4. Serverul salveaza evenimentele in baza de date
  5. Scriptul checkIMEvents_p.php, rulat ca proces prin CRON, selecteaza ultimele evenimente
     din baza de date
  6. Le trimite la serverul de IM si le sterge pe cele efectuate cu succes din baza de date

                                                       Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 18
Tehnici Php: Manipularea Proceselor April 25, 2009


Exemplu pentru modelul anterior:

checkIMEvents_p.php
<?php                                                                // Verificam conexiunea la db
// Folosim functiile pt verificarea pid-ului si                      if ($evt->checkDb()) {
talk                                                                    // Trimitem evenimentele apoi le stergem
                                                                        $evt->handleEvents();
require_once(“processFunctions.php”);
                                                                        sleep(2);
talk($pid);
                                                                     }
// Verificam pid-ul din fisier                                       else {
if (!checkPidFromFile()) {                                              exit();
   // In cazul in care nu exista/nu ruleaza, il scriem               }
   writePid();                                                   }
   $evt = new IMEvents();                                    }

  while(1) {



                                                                 Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 19
Tehnici Php: Manipularea Proceselor April 25, 2009




Beneficii:

     Notificarea rapida a serverului IM
     Efectuare aproape in timp real a evenimentelor datorita rularii in paralel
     Izolare in cazul erorilor de server
     Contorizare a evenimentelor




                                                          Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 20
Tehnici Php: Manipularea Proceselor April 25, 2009


Concluzii:




  O alternativa eficienta a metodelor de programare secventiala
  Posibilitatea izolarii unui task care necesita mai mult timp de executie
  Implementare usoara, 90% in mediul Php
  Ofera modalitati de supraveghere constanta si facila
  Posibilitatea terminarii unui task esuat
  Suport maxim pe sistemele de operare Unix/Linux
  Avantajul rularii continue(actualizare continua a datelor)
  Respecta ideea de “unu’ si bun”




                                                        Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 21

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Tudor Popa - Tehnici PHP: Manipularea proceselor

  • 1. Tehnici Php: Manipularea Proceselor April 25, 2009 Tehnici Php: Manipularea proceselor Tudor Popa Web2.0 Jedi Server + Client Side Efficiency Yoda Wannabe Semantic Web Jedi Wixi.com Jedi Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 1
  • 2. Tehnici Php: Manipularea Proceselor April 25, 2009 Lucrul cu procese in Php(contra):  “Cam greu de implementat pe WAMP!”  “De ce sa ma complic cu procese cand pot face totul din acelasi script?!”  “Exista riscul folosirii excesive a memoriei din cauza aparitiei mai multor instante!!!”  “Cum pot sti daca ruleaza sau nu?!”  “Mai bine ma folosesc de CRON!”  “Daca ruleaza la nesfarsit si nu il pot stapani??”  “Credeam ca am scapat de procese cand am terminat-o cu C-ul!”  “Procese?! Ba, credeam ca suntem la GeekMeet, nu la tribunal…” Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 2
  • 3. Tehnici Php: Manipularea Proceselor April 25, 2009 Modalitati de abordare PCNTL: Respecta stilul Unix in:  Crearea proceselor  Executia programului  Manipularea semnalelor(cu ajutorul “ticks”-urilor)  Terminarea proceselor  Semantica Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 3
  • 4. Tehnici Php: Manipularea Proceselor April 25, 2009 Mic exemplu cu fork <?php // Facem fork $pid = pcntl_fork(); if ($pid == -1) { die(quot;Eroare la fork()quot;); } else if ($pid) { // Suntem in Parinte print “Parintele aici, PID-ul fiului este: $pidn”; } else { // Suntem in Fiu print “Fiul aici!n”; } Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 4
  • 5. Tehnici Php: Manipularea Proceselor April 25, 2009 Mic exemplu cu tratarea semnalelor <?php case SIGTERM: declare(ticks=1); // Semnal de shutdown // Atribuim handlerul de semnale exit; pcntl_signal(SIGTERM, quot;sig_handlerquot;); break; pcntl_signal(SIGHUP, quot;sig_handlerquot;); case SIGHUP: // Procesul propriu-zis // Semnal de restart while (1) { break; // Executa ceva default: } // Alte semnale // Handlerul de semnale } function sig_handler($signo) { } switch ($signo) { Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 5
  • 6. Tehnici Php: Manipularea Proceselor April 25, 2009 Rulare “manuala” prin Shell sau CRON:  Administrare la nivel de consola  Posibilitate de “supraveghere” (top, htop, supervise, etc.)  Avantajul executarii unui singur task  Avantajul rularii continue(daca se foloseste rularea prin CRON)  Easy to strace Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 6
  • 7. Tehnici Php: Manipularea Proceselor April 25, 2009 Mic exemplu de proces pornit din Shell processTest.php <?php $evt = new UserEventsHandler(); while(1) { if ($evt->checkDb()) { $evt->handleEvents(); sleep(2); } else { exit(); } } Consola: Cd <path_to_file>; php5 processTest.php Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 7
  • 8. Tehnici Php: Manipularea Proceselor April 25, 2009 Probleme legate de exemplul anterior  Posibilitatea aparitiei mai multor instante(in cazul rularii prin CRON) Rezolvare: // Verificarea pid-ului function checkPid($pid) { $cmd = quot;ps -o stat --no-headers $pidquot;; exec($cmd, $output, $result); if(count($output) >= 1){ return true; } return false; } Output consola $ ps -o stat --no-headers 6730 S+ Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 8
  • 9. Tehnici Php: Manipularea Proceselor April 25, 2009 Folosing functia anterioara, verificam pid-ul salvat intr-un fisier function checkPidFromFile() { return false; if (file_exists(PATH_PID)) { } $fh = fopen(PATH_PID, 'r'); } $data = fgets($fh); else { return true; if ($data != null) { } $pid = intval($data); } if (checkPid($pid)) { else { fclose($fh); return false; return true; } } } else { Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 9
  • 10. Tehnici Php: Manipularea Proceselor April 25, 2009 Functia care scrie pid-ul curent intr-un fisier function writePid() { $pid = getmypid(); $fh = fopen(PATH_ PID, 'w+'); fwrite($fh, strval($pid)); fclose($fh); } Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 10
  • 11. Tehnici Php: Manipularea Proceselor April 25, 2009  Lipsa comunicarii(no output, “Silence is golden”, dar nu tot timpul) Rezolvare: function talk($pid) { if (isset($_GET['status'])) { // Afisam date despre proces ProcessHandler::tellProcessStatus($pid); die(); } } Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 11
  • 12. Tehnici Php: Manipularea Proceselor April 25, 2009 Varianta imbunatatita // Verificam pid-ul din fisier talk($pid); if (!checkPidFromFile()) { // In cazul in care nu exista/nu ruleaza, il scriem writePid(); $evt = new UserEventsHandler(); while(1) { if ($evt->checkDb()) { $evt->handleEvents(); sleep(2); } else { exit(); } } } Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 12
  • 13. Tehnici Php: Manipularea Proceselor April 25, 2009 Cazuri favorabile utilizarii proceselor  Data caching(exemplu Memcached) Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 13
  • 14. Tehnici Php: Manipularea Proceselor April 25, 2009 Scenariu: 1. Userul cere date de la data controller 2. Data controller-ul cauta in cache a. Daca rezultatele exista, le returneaza b. Daca nu, le ia din baza de date (,iar apoi le introduce in cache) si le returneaza 3. Scriptul refreshData_p.php, rulat ca proces prin CRON, introduce continuu in cache date din baza de date Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 14
  • 15. Tehnici Php: Manipularea Proceselor April 25, 2009 Exemplu pentru modelul anterior: refreshData_p.php <?php // Folosim functiile pt verificarea pid-ului si CacheHandler::addToCache(QueryChecker::retur talk nNewQueryData); sleep(2); require_once(“processFunctions.php”); } talk($pid); else { // Verificam pid-ul din fisier exit(); if (!checkPidFromFile()) { } // In cazul in care nu exista/nu ruleaza, il scriem } writePid(); } while(1) { if (QueryChecker::isNewDataInQueries()) { Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 15
  • 16. Tehnici Php: Manipularea Proceselor April 25, 2009 Beneficii:  Mentinerea continua a datelor noi in cache  Izolare in cazul erorilor de server/umane  Update la interval de timp scazut datorita executarii unui singur task Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 16
  • 17. Tehnici Php: Manipularea Proceselor April 25, 2009  External server calls(exemplu community im) Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 17
  • 18. Tehnici Php: Manipularea Proceselor April 25, 2009 Scenariu: 1. Userul se logheaza in site 2. Executa evenimente: a. Schimba avatar-ul b. Schimba nickname-ul c. Schimba informatii personale d. Adauga prieteni e. Sterge prieteni f. Log-out 3. Serverul trimite evenimentele user-ului la serverul de IM si is continua taskurile 4. Serverul salveaza evenimentele in baza de date 5. Scriptul checkIMEvents_p.php, rulat ca proces prin CRON, selecteaza ultimele evenimente din baza de date 6. Le trimite la serverul de IM si le sterge pe cele efectuate cu succes din baza de date Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 18
  • 19. Tehnici Php: Manipularea Proceselor April 25, 2009 Exemplu pentru modelul anterior: checkIMEvents_p.php <?php // Verificam conexiunea la db // Folosim functiile pt verificarea pid-ului si if ($evt->checkDb()) { talk // Trimitem evenimentele apoi le stergem $evt->handleEvents(); require_once(“processFunctions.php”); sleep(2); talk($pid); } // Verificam pid-ul din fisier else { if (!checkPidFromFile()) { exit(); // In cazul in care nu exista/nu ruleaza, il scriem } writePid(); } $evt = new IMEvents(); } while(1) { Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 19
  • 20. Tehnici Php: Manipularea Proceselor April 25, 2009 Beneficii:  Notificarea rapida a serverului IM  Efectuare aproape in timp real a evenimentelor datorita rularii in paralel  Izolare in cazul erorilor de server  Contorizare a evenimentelor Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 20
  • 21. Tehnici Php: Manipularea Proceselor April 25, 2009 Concluzii:  O alternativa eficienta a metodelor de programare secventiala  Posibilitatea izolarii unui task care necesita mai mult timp de executie  Implementare usoara, 90% in mediul Php  Ofera modalitati de supraveghere constanta si facila  Posibilitatea terminarii unui task esuat  Suport maxim pe sistemele de operare Unix/Linux  Avantajul rularii continue(actualizare continua a datelor)  Respecta ideea de “unu’ si bun” Tudor Popa | tdr.popa@gmail.com | tudor@wixi.com 21