SlideShare a Scribd company logo
1 of 11
Download to read offline
Współbieżność
Jakub Nietrzeba
TAK
●
Dużo łatwych do wydzielenia odrębnych zadań
– Obliczenia
– Pobieranie plików, dręczenie SOAPa
● Potężny serwer, dużo RAMu
● Lepsze zagospodarowanie sprzętu i łącza
NIE
● Operacje na bazach danych
● Operacje na dysku
● Serwery oparte o gniazdka (select!)
● Może biblioteka zewnętrzna już to ma? (np. GraphicsMagick do
zdjęć)
● Koszty utrzymania rosną!
Komunikacja z wątkiem
● Kolejki komunikatów (msg_get_queue,
msg_receive)
– Ograniczenia rozmiaru! (do obejścia)
– Autoserializacja (uwaga na klasy!)
– Protip: ftok używa tylko jednego znaku!
● Dzielona pamięć (shmop_open, shmop_read)
– „Prawie” plik, bardziej zmienna
– Trzeba synchronizować zapis/odczyt
– Dane muszą mieścić się w pamięci
Komunikacja z wątkiem
● Potoki nazwane (posix_mkfifo)
– Zachowują się jak pliki (fread, fwrite)
– Warto używać selecta (stream_select)
– Rozmiar bufora 65KB (1 MB max!)
● Sygnały
● Semafory, mutexy
Sposoby
● pthreads
● pcntl_fork
● proc_open/popen
● inne (magia!)
pthreads
● „Prawdziwe” wątki
● Pełna gama gotowych klas (Wątki, Workery, Pool…)
● Uruchamiane na żądanie, można usypiać, wznawiać
● Wymagają rekompilacji PHP
● Synchronizacja! Serializacja!
● Nie wszystko działa jak powinno
– Kopiowanie uchwytów do zasobów rodzi bugi
– Classloader zanika
pthreads
<?php
class Watek extends Thread {
public function run() {
// działamy działamy
}
}
$w = new Watek();
$w->z = 123;
$w->start();
pcntl_fork
● Najstarszy mechanizm wątków
● Działa wszędzie (czyli nie na Windowsie)
● Łatwe w uruchomieniu, trudne w synchronizacji
● Robi się trudno gdy chcemy mieć więcej dzieci
● Wątek od razu się uruchamia
● Komunikacja z wątkiem jest na naszej głowie
● Kopiowanie zasobów!
● Łase na pamięć
pcntl_fork
<?php
$pid = pcntl_fork();
if($pid < 0) die('Uuuups');
if($pid) {
//tu wpadnie w wątku rodzica
pcntl_wait($status); //czekamy aż dziecko skończy
} else {
//tu się toczy wątek dziecka, działamy!
}
proc_open
● „Prawie” to samo co fork
● Uruchamia się nowy proces
● Uniwersalne
● Łase na pamięć
● Bardzo przydatne strumienie wejścia/wyjścia
● Skomplikowane uruchamianie i zarządzanie
● Nice! (proc_nice)
Magia!
● curl_multi_init
● win32service
● Demonizacja

More Related Content

Featured

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
Kurio // The Social Media Age(ncy)
 

Featured (20)

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...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Współbieżność w PHP

  • 2. TAK ● Dużo łatwych do wydzielenia odrębnych zadań – Obliczenia – Pobieranie plików, dręczenie SOAPa ● Potężny serwer, dużo RAMu ● Lepsze zagospodarowanie sprzętu i łącza NIE ● Operacje na bazach danych ● Operacje na dysku ● Serwery oparte o gniazdka (select!) ● Może biblioteka zewnętrzna już to ma? (np. GraphicsMagick do zdjęć) ● Koszty utrzymania rosną!
  • 3. Komunikacja z wątkiem ● Kolejki komunikatów (msg_get_queue, msg_receive) – Ograniczenia rozmiaru! (do obejścia) – Autoserializacja (uwaga na klasy!) – Protip: ftok używa tylko jednego znaku! ● Dzielona pamięć (shmop_open, shmop_read) – „Prawie” plik, bardziej zmienna – Trzeba synchronizować zapis/odczyt – Dane muszą mieścić się w pamięci
  • 4. Komunikacja z wątkiem ● Potoki nazwane (posix_mkfifo) – Zachowują się jak pliki (fread, fwrite) – Warto używać selecta (stream_select) – Rozmiar bufora 65KB (1 MB max!) ● Sygnały ● Semafory, mutexy
  • 5. Sposoby ● pthreads ● pcntl_fork ● proc_open/popen ● inne (magia!)
  • 6. pthreads ● „Prawdziwe” wątki ● Pełna gama gotowych klas (Wątki, Workery, Pool…) ● Uruchamiane na żądanie, można usypiać, wznawiać ● Wymagają rekompilacji PHP ● Synchronizacja! Serializacja! ● Nie wszystko działa jak powinno – Kopiowanie uchwytów do zasobów rodzi bugi – Classloader zanika
  • 7. pthreads <?php class Watek extends Thread { public function run() { // działamy działamy } } $w = new Watek(); $w->z = 123; $w->start();
  • 8. pcntl_fork ● Najstarszy mechanizm wątków ● Działa wszędzie (czyli nie na Windowsie) ● Łatwe w uruchomieniu, trudne w synchronizacji ● Robi się trudno gdy chcemy mieć więcej dzieci ● Wątek od razu się uruchamia ● Komunikacja z wątkiem jest na naszej głowie ● Kopiowanie zasobów! ● Łase na pamięć
  • 9. pcntl_fork <?php $pid = pcntl_fork(); if($pid < 0) die('Uuuups'); if($pid) { //tu wpadnie w wątku rodzica pcntl_wait($status); //czekamy aż dziecko skończy } else { //tu się toczy wątek dziecka, działamy! }
  • 10. proc_open ● „Prawie” to samo co fork ● Uruchamia się nowy proces ● Uniwersalne ● Łase na pamięć ● Bardzo przydatne strumienie wejścia/wyjścia ● Skomplikowane uruchamianie i zarządzanie ● Nice! (proc_nice)