Share
Embed
Paralelismo e Concorrência em Python
Discussão sobre os recursos nativos de Python para programação paralela e gerenciamento de concorrência.
Tópicos dessa palestra:
- Threads
- Global Interpreter Lock (GIL)
- Multi-processamento
- Sincronização
Tema principal: Científico
Nível: Intermediário/Avançado
Wagner Macedo
July 12, 2016
Transcript
32 slides
-
AGENDA Conceitos Básicos Threads em Python Threads e Execução Paralela
Multi-processamento Sincronização -
Thread 1 Thread 2 Thread 1 Thread 2 Paralelismo x
Concorrência -
t h r e a d i n g .
T h r e a d s t a r t ( ) Inicia as ações da thread r u n ( ) Ações da thread a serem executadas j o i n ( t i m e o u t = N o n e ) Bloqueia até a thread encerrar i s _ a l i v e ( ) Retorna se a thread está viva d a e m o n Indicador se a thread é do tipo daemon -
EXEMPLO 1 f r o m t h r e
a d i n g i m p o r t T h r e a d c l a s s M y T h r e a d ( T h r e a d ) : d e f r u n ( ) : # f a z a l g o t = M y T h r e a d ( ) t . s t a r t ( ) -
EXEMPLO 2 f r o m t h r e
a d i n g i m p o r t T h r e a d d e f f u n c ( ) : # f a z a l g o t = T h r e a d ( t a r g e t = f u n c ) t . s t a r t ( ) -
EXEMPLO 3 f r o m t h r e
a d i n g i m p o r t T h r e a d d e f o p e r a c a o ( o p 1 , o p 2 , t i p o = " s o m a " ) : # i m p l e m e n t a ç ã o t = T h r e a d ( t a r g e t = f u n c , a r g s = [ 5 , 2 ] , k w a r g s = { ' t i p o ' : " s u b t r a ç ã o " } ) t . s t a r t ( ) -
EXEMPLO 4 f r o m t h r e
a d i n g i m p o r t T h r e a d T h r e a d ( ) . s t a r t ( ) d e f f u n c 1 ( ) : p r i n t ( " H e l l o " ) d e f f u n c 2 ( ) : p r i n t ( " W o r l d " ) T h r e a d ( t a r g e t = f u n c 1 ) . s t a r t ( ) T h r e a d ( t a r g e t = f u n c 2 ) . s t a r t ( ) -
EXEMPLO 4 (MODIFICADO) f r o m t h r
e a d i n g i m p o r t T h r e a d n s = [ 0 ] d e f f u n c 1 ( ) : f o r i i n r a n g e ( 5 0 0 0 0 ) : # A ç õ e s e x t r a s n s [ 0 ] = n s [ 0 ] + 1 # n a t h r e a d 1 ! p r i n t ( " H e l l o " ) d e f f u n c 2 ( ) : p r i n t ( " W o r l d " ) T h r e a d ( t a r g e t = f u n c 1 ) . s t a r t ( ) T h r e a d ( t a r g e t = f u n c 2 ) . s t a r t ( ) p r i n t ( n s ) # A ç ã o n a t h r e a d p r i n c i p a l -
THREADS PARECEM RODAR EM PARALELO i m p o r
t t i m e f r o m t h r e a d i n g i m p o r t T h r e a d T h r e a d ( t a r g e t = t i m e . s l e e p , a r g s = [ 3 ] ) . s t a r t ( ) T h r e a d ( t a r g e t = t i m e . s l e e p , a r g s = [ 4 ] ) . s t a r t ( ) Quanto tempo dura esse programa? -
MAS NÃO RODAM! f r o m t h r
e a d i n g i m p o r t T h r e a d d e f f i b ( n ) : i f n = = 1 o r n = = 2 : r e t u r n 1 r e t u r n f i b ( n 1 ) + f i b ( n 2 ) T h r e a d ( t a r g e t = f i b , a r g s = [ 3 1 ] ) . s t a r t ( ) T h r e a d ( t a r g e t = f i b , a r g s = [ 3 1 ] ) . s t a r t ( ) E agora? Quanto tempo dura esse programa? -
GLOBAL INTERPRETER LOCK (GIL) Característica de implementação do CPython. Somente
uma thread pode executar código Python por vez. As threads não são boas para tarefas orientadas à CPU Mas as threads são ótimas para tarefas ligadas à I/O -
m u l t i p r o c e
s s i n g . P r o c e s s s t a r t ( ) Inicia as ações do processo r u n ( ) Ações do processo a serem executadas j o i n ( t i m e o u t = N o n e ) Bloqueia até o processo encerrar i s _ a l i v e ( ) Retorna se o processo está vivo d a e m o n Indicador se o processo é do tipo daemon API idêntica à t h r e a d i n g . T h r e a d -
m u l t i p r o c e
s s i n g . P r o c e s s (extra) p i d Retorna o ID do processo e x i t c o d e O código de saída do processo a u t h k e y Chave de segurança para a troca de mensagens entre processos t e r m i n a t e ( ) Encerra o processo, abruptamente! -
MÉTODOS DE INICIALIZAÇÃO spawn Todos os objetos precisam ser "picklables".
(Windows / Unix) fork Mais rápido, sem exigência de "picklable". (Unix) forkserver Inicia um servidor de forks local. Todos os objetos precisam ser "picklables". (Unix) -
PROCESSOS SÃO INÚTEIS EM TAREFAS DE I/O i m p
o r t t i m e f r o m m u l t i p r o c e s s i n g i m p o r t P r o c e s s P r o c e s s ( t a r g e t = t i m e . s l e e p , a r g s = [ 3 ] ) . s t a r t ( ) P r o c e s s ( t a r g e t = t i m e . s l e e p , a r g s = [ 4 ] ) . s t a r t ( ) -
MAS SÃO EXCELENTES EM TAREFAS DE CPU! f r o
m m u l t i p r o c e s s i n g i m p o r t P r o c e s s d e f f i b ( n ) : i f n = = 1 o r n = = 2 : r e t u r n 1 r e t u r n f i b ( n 1 ) + f i b ( n 2 ) P r o c e s s ( t a r g e t = f i b , a r g s = [ 3 1 ] ) . s t a r t ( ) P r o c e s s ( t a r g e t = f i b , a r g s = [ 3 1 ] ) . s t a r t ( ) -
COMPATILHAMENTO ENTRE PROCESSOS m u l t i p r
o c e s s i n g . V a l u e valor em memória compartilhada de um tipo especí co m u l t i p r o c e s s i n g . A r r a y array em memória compartilhada de um tipo especí co m u l t i p r o c e s s i n g . M a n a g e r processo servidor que mantém objetos "picklables" -
m u l t i p r o c e
s s i n g . P o o l f r o m m u l t i p r o c e s s i n g i m p o r t P o o l d e f f ( x ) : r e t u r n x * x # s t a r t 4 w o r k e r p r o c e s s e s w i t h P o o l ( p r o c e s s e s = 4 ) a s p o o l : # p r i n t " [ 0 , 1 , 4 , . . . , 8 1 ] " p r i n t ( p o o l . m a p ( f , r a n g e ( 1 0 ) ) ) -
LOCKS f r o m t h r e a
d i n g i m p o r t L o c k # O U f r o m m u l t i p r o c e s s i n g i m p o r t L o c k l o c k = L o c k ( ) d e f f u n c a o _ c o n c o r r e n t e ( ) : t r y : l o c k . a c q u i r e ( ) # f a z a l g u m a c o i s a f i n a l l y : l o c k . r e l e a s e ( ) -
Usando Locks com w i t h l o c
k = L o c k ( ) d e f f u n c a o _ c o n c o r r e n t e ( ) : w i t h l o c k : # f a z a l g u m a c o i s a -
OUTRAS PRIMITIVAS DE SINCRONIZAÇÃO R L o c k C
o n d i t i o n S e m a p h o r e E v e n t T i m e r B a r r i e r