Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def recur_fibo(n):
- """Recursive function to
- print Fibonacci sequence"""
- if n <= 1:
- return n
- else:
- return(recur_fibo(n-1) + recur_fibo(n-2))
- recur_fibo(33)
- recur_fibo(40)
- recur_fibo(100)
- from multiprocessing import Pool
- def fib(n):
- if n <= 1:
- return n
- else:
- return(fib(n-1) + fib(n-2))
- try:
- p = Pool()
- print (p.map(fib, [10, 15, 20]))
- finally:
- p.close()
- import Queue, threading
- q = Queue.Queue()
- def fib(n):
- a, b = 0, 1
- for i in range(0, n):
- a, b = b, a + b
- q.put((n, a))
- return
- numeros = [10, 20, 25]
- for n in numeros:
- t = threading.Thread(target=fib, args = (n,))
- t.daemon = True
- t.start()
- while not q.empty():
- n, f = q.get()
- print ("{0}: {1}".format(n, f))
- from concurrent.futures import ThreadPoolExecutor, as_completed
- def fib(n):
- a, b = 0, 1
- for i in range(0, n):
- a, b = b, a + b
- return ((n, a))
- numeros = [10, 20, 25]
- with ThreadPoolExecutor(max_workers=5) as executor:
- fibSubmit = {executor.submit(fib, n,): n for n in numeros}
- for future in as_completed(fibSubmit):
- try:
- n, f = future.result()
- except Exception as exc:
- print("Erro! {0}".format(exc))
- else:
- print ("{0}: {1}".format(n, f))
- import threading
- def fibo(n):
- a, b = 0, 1
- for i in range(n):
- a, b = b, a+b
- data[threading.currentThread().getName()] = a
- def get_results():
- 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
- continue
- return data
- data = {} # armazenar os dados
- fibos = [10, 15, 20, 30]
- for k, v in enumerate(fibos, 1):
- t = threading.Thread(target=fibo, args=(v,), name='t_{}'.format(k)) # preparar cada thread
- t.start() # começar cada thread
- print(get_results()) # trabalho feito por cada thread
- import threading, queue
- def fibo(n):
- a, b = 0, 1
- for i in range(n):
- a, b = b, a+b
- data[threading.currentThread().getName()] = a
- if(threading.active_count() == 2): # se só houverem 2 threads esta e a main
- data_q.put(True)
- data_q = queue.Queue()
- data = {} # armazenar os dados
- fibos = [10, 15, 20, 30]
- for k, v in enumerate(fibos, 1):
- t = threading.Thread(target=fibo, args=(v,), name='t_{}'.format(k)) # preparar cada thread
- t.start() # começar cada thread
- data_q.get() # bloquear aqui ate receber 'sinal'
- print(data) # trabalho feito por cada thread
Add Comment
Please, Sign In to add comment