Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/04/2013, 06:52
ncw2233
 
Fecha de Ingreso: abril-2011
Mensajes: 12
Antigüedad: 13 años
Puntos: 0
Sonrisa fork o usar Thread

Hola he estado tratando de comprender las diferencias entre los fork y los theread en python :
y entiendo lo siguiente :

-Thread son hilos de un proceso que comparten memoria
- fork , son clones o copias de un proceso

lo que yo intento hacer es varios fork para un proceso y eh visto este pequeño script (fuente :http://goo.gl/kwkXC ) al cual le he agregado un random .. pero no ocurre el random ._.
es decir creo los 7 fork y me devuelve lo mismo , eso me da la siguiente duda "random no es tan random xD" o los fork comparten variables. tambien puede ser que no comprenda algun detalle de los fork ,

de ante mano gracias.



Código Python:
Ver original
  1. #! /usr/bin/python
  2. import os
  3. import random
  4. def child():
  5.    print 'A new child ',  os.getpid( )
  6.    print random.random()
  7.    print random.randrange(0, 101, 2)
  8.    os._exit(0)
  9.  
  10. def parent():
  11.    i=0
  12.    while True:
  13.       newpid = os.fork()
  14.       if newpid == 0:
  15.          child()
  16.       else:
  17.          pids = (os.getpid(), newpid)
  18.          print "parent: %d, child: %d" % pids
  19.       if  i== 7:
  20.         break
  21.       i=i+1
  22.  
  23. parent()
resultados :
Código BASH:
Ver original
  1. -parent: 8836, child: 8837
  2. A new child  8837
  3. 0.773740529819
  4. parent: 8836, child: 8838
  5. 42
  6. parent: 8836, child: 8839
  7. A new child  8839
  8. 0.773740529819
  9. parent: 8836, child: 8840
  10. 42
  11. parent: 8836, child: 8841
  12. A new child  8840
  13. parent: 8836, child: 8842
  14. 0.773740529819
  15. 42
  16. parent: 8836, child: 8843
  17. A new child  8842
  18. parent: 8836, child: 8844
  19. 0.773740529819
  20. 42
  21. A new child  8844
  22. 0.773740529819
  23. 42
  24. A new child  8843
  25. 0.773740529819
  26. 42
  27. A new child  8841
  28. 0.773740529819
  29. 42
  30. A new child  8838
  31. 0.773740529819
  32. 42