globo
.com Paralelismo em Python
what the f**rancisco?!
‣ Globo.com
‣ #cobrateam
‣ Open Source fanboy
Por que?
The free lunch is over
“Most classes of applications have
enjoyed free and regular performance
gains for several decades, even without
releasing ...
Lei de Moore
Python?
https://secure.flickr.com/photos/infidelic/4306205887/
Global Interpreter Lock
Thread 1
Thread 2
interpreter.lock()
interpreter.run(code1)
interpreter.unlock()
interpreter.lock()
interpreter.run(code2)...
Py_BEGIN_ALLOW_THREADS
Py_END_ALLOW_THREADS
Py_BEGIN_ALLOW_THREADS
/*	
  danger	
  zone	
  */
Py_END_ALLOW_THREADS
https://secure.flickr.com/photos/stankuns/5260301627/
https://secure.flickr.com/photos/shucker/5132309573/
Multiprocessing
https://secure.flickr.com/photos/wooleyduck/79215076/
def	
  sum_values(values,	
  res_dict,	
  key):
	
  	
  	
  	
  res_dict[key]	
  =	
  sum(values)
if	
  __name__	
  ==	
  ...
 per_thread	
  =	
  len(values)	
  /	
  N_THREADS
	
  for	
  i	
  in	
  range(N_THREADS):
	
  	
  	
  	
  	
  start	
  =	
...
https://secure.flickr.com/photos/alc_chris/9887291126/
https://secure.flickr.com/photos/artbystevejohnson/6673406227/
http://blog.afkham.org/2013/02/raspberry-pi-cluster-images.html
iPython
https://secure.flickr.com/photos/ravages/9329463362/
https://secure.flickr.com/photos/osde-info/8626662243/
concurrent.futures
urls	
  =	
  ("http://2013.ploneconf.org/",	
  "http://2013.pythonbrasil.org.br/",	
  ...)
executor	
  =	
  concurrent.fut...
urls	
  =	
  ("http://2013.ploneconf.org/",	
  "http://2013.pythonbrasil.org.br/",	
  ...)
executor	
  =	
  concurrent.fut...
https://secure.flickr.com/photos/kalexanderson/5421517469/
Backport
‣ pip install futures
‣ s/concurrent.futures/futures/
Onemorething...
Crie tendência,
venha construir o
futuro da internet
usando Python.
G1
globoesporte.com
Novelas
Globo.com
Musica.com.br
Te...
Paralelismo em
Python
Francisco Souza
fss@corp.globo.com
@franciscosouza
slideshare.net/franciscosouza
globo
.com
Paralelismo em Python
Paralelismo em Python
Paralelismo em Python
Paralelismo em Python
Paralelismo em Python
Paralelismo em Python
Upcoming SlideShare
Loading in …5
×

Paralelismo em Python

1,820 views

Published on

Palestra apresentada na PythonBrasil[9], sobre paralelismo em Python.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,820
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Paralelismo em Python

  1. 1. globo .com Paralelismo em Python
  2. 2. what the f**rancisco?! ‣ Globo.com ‣ #cobrateam ‣ Open Source fanboy
  3. 3. Por que?
  4. 4. The free lunch is over
  5. 5. “Most classes of applications have enjoyed free and regular performance gains for several decades, even without releasing new versions or doing anything special... - Herb Sutter, 2005
  6. 6. Lei de Moore
  7. 7. Python?
  8. 8. https://secure.flickr.com/photos/infidelic/4306205887/ Global Interpreter Lock
  9. 9. Thread 1 Thread 2 interpreter.lock() interpreter.run(code1) interpreter.unlock() interpreter.lock() interpreter.run(code2) interpreter.unlock()
  10. 10. Py_BEGIN_ALLOW_THREADS Py_END_ALLOW_THREADS
  11. 11. Py_BEGIN_ALLOW_THREADS /*  danger  zone  */ Py_END_ALLOW_THREADS
  12. 12. https://secure.flickr.com/photos/stankuns/5260301627/
  13. 13. https://secure.flickr.com/photos/shucker/5132309573/
  14. 14. Multiprocessing
  15. 15. https://secure.flickr.com/photos/wooleyduck/79215076/
  16. 16. def  sum_values(values,  res_dict,  key):        res_dict[key]  =  sum(values) if  __name__  ==  "__main__":        result  =  {}        values  =  range(10000)        threads  =  []        per_thread  =  len(values)  /  N_THREADS        for  i  in  range(N_THREADS):                start  =  i  *  per_thread                t  =  threading.Thread(target=sum_values,  args=(values[start:start  +  per_thread],                                                                                                            result,                                                                                                            "t{0:d}".format(i+1)))                t.start()                threads.append(t)        for  t  in  threads:                t.join()
  17. 17.  per_thread  =  len(values)  /  N_THREADS  for  i  in  range(N_THREADS):          start  =  i  *  per_thread          t  =  threading.Thread(target=sum_values,  args=(va                                                                                                      re                                                                                                      "t          t.start()          threads.append(t)  for  t  in  threads:          t.join()
  18. 18. https://secure.flickr.com/photos/alc_chris/9887291126/
  19. 19. https://secure.flickr.com/photos/artbystevejohnson/6673406227/
  20. 20. http://blog.afkham.org/2013/02/raspberry-pi-cluster-images.html
  21. 21. iPython
  22. 22. https://secure.flickr.com/photos/ravages/9329463362/
  23. 23. https://secure.flickr.com/photos/osde-info/8626662243/
  24. 24. concurrent.futures
  25. 25. urls  =  ("http://2013.ploneconf.org/",  "http://2013.pythonbrasil.org.br/",  ...) executor  =  concurrent.futures.ThreadPoolExecutor(max_workers=8) futures  =  [executor.submit(load,  url,  60)  for  url  in  urls] for  f  in  concurrent.futures.as_completed(futures):        print(f.result())
  26. 26. urls  =  ("http://2013.ploneconf.org/",  "http://2013.pythonbrasil.org.br/",  ...) executor  =  concurrent.futures.ThreadPoolExecutor(max_workers=8) futures  =  [executor.submit(load,  url,  60)  for  url  in  urls] for  f  in  concurrent.futures.as_completed(futures):        print(f.result()) urls  =  ("http://2013.ploneconf.org/",  "http://2013.pythonbrasil.org.br/",  ...) executor  =  concurrent.futures.ProcessPoolExecutor(max_workers=8) futures  =  [executor.submit(load,  url,  60)  for  url  in  urls] for  f  in  concurrent.futures.as_completed(futures):        print(f.result())
  27. 27. https://secure.flickr.com/photos/kalexanderson/5421517469/
  28. 28. Backport ‣ pip install futures ‣ s/concurrent.futures/futures/
  29. 29. Onemorething...
  30. 30. Crie tendência, venha construir o futuro da internet usando Python. G1 globoesporte.com Novelas Globo.com Musica.com.br TechTudo ... globo.com/talentos globo .com Venha trabalhar com a gente Você cria, 45 milhões usam diariamente
  31. 31. Paralelismo em Python Francisco Souza fss@corp.globo.com @franciscosouza slideshare.net/franciscosouza globo .com

×