Guest User

Untitled

a guest
Mar 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. def recur_fibo(n):
  2. """Recursive function to
  3. print Fibonacci sequence"""
  4. if n <= 1:
  5. return n
  6. else:
  7. return(recur_fibo(n-1) + recur_fibo(n-2))
  8.  
  9. recur_fibo(33)
  10. recur_fibo(40)
  11. recur_fibo(100)
  12.  
  13. from multiprocessing import Pool
  14.  
  15. def fib(n):
  16. if n <= 1:
  17. return n
  18. else:
  19. return(fib(n-1) + fib(n-2))
  20.  
  21. try:
  22. p = Pool()
  23. print (p.map(fib, [10, 15, 20]))
  24. finally:
  25. p.close()
  26.  
  27. import Queue, threading
  28.  
  29. q = Queue.Queue()
  30.  
  31. def fib(n):
  32. a, b = 0, 1
  33. for i in range(0, n):
  34. a, b = b, a + b
  35. q.put((n, a))
  36. return
  37.  
  38. numeros = [10, 20, 25]
  39.  
  40. for n in numeros:
  41. t = threading.Thread(target=fib, args = (n,))
  42. t.daemon = True
  43. t.start()
  44.  
  45. while not q.empty():
  46. n, f = q.get()
  47. print ("{0}: {1}".format(n, f))
  48.  
  49. from concurrent.futures import ThreadPoolExecutor, as_completed
  50.  
  51. def fib(n):
  52. a, b = 0, 1
  53. for i in range(0, n):
  54. a, b = b, a + b
  55. return ((n, a))
  56.  
  57. numeros = [10, 20, 25]
  58.  
  59. with ThreadPoolExecutor(max_workers=5) as executor:
  60. fibSubmit = {executor.submit(fib, n,): n for n in numeros}
  61.  
  62. for future in as_completed(fibSubmit):
  63. try:
  64. n, f = future.result()
  65. except Exception as exc:
  66. print("Erro! {0}".format(exc))
  67. else:
  68. print ("{0}: {1}".format(n, f))
  69.  
  70. import threading
  71.  
  72. def fibo(n):
  73. a, b = 0, 1
  74. for i in range(n):
  75. a, b = b, a+b
  76. data[threading.currentThread().getName()] = a
  77.  
  78. def get_results():
  79. while(threading.active_count() > 1): # enquanto houverem threads a trabalhar vais esperar, a main thread conta como 1, quando as threads acabarem vais retornar os dados
  80. continue
  81. return data
  82.  
  83. data = {} # armazenar os dados
  84. fibos = [10, 15, 20, 30]
  85. for k, v in enumerate(fibos, 1):
  86. t = threading.Thread(target=fibo, args=(v,), name='t_{}'.format(k)) # preparar cada thread
  87. t.start() # começar cada thread
  88.  
  89. print(get_results()) # trabalho feito por cada thread
  90.  
  91. import threading, queue
  92.  
  93. def fibo(n):
  94. a, b = 0, 1
  95. for i in range(n):
  96. a, b = b, a+b
  97. data[threading.currentThread().getName()] = a
  98. if(threading.active_count() == 2): # se só houverem 2 threads esta e a main
  99. data_q.put(True)
  100.  
  101. data_q = queue.Queue()
  102. data = {} # armazenar os dados
  103. fibos = [10, 15, 20, 30]
  104. for k, v in enumerate(fibos, 1):
  105. t = threading.Thread(target=fibo, args=(v,), name='t_{}'.format(k)) # preparar cada thread
  106. t.start() # começar cada thread
  107.  
  108. data_q.get() # bloquear aqui ate receber 'sinal'
  109. print(data) # trabalho feito por cada thread
Add Comment
Please, Sign In to add comment