fork download
  1. import threading, queue
  2.  
  3. def fibo(n):
  4. a, b = 0, 1
  5. for i in range(n):
  6. a, b = b, a+b
  7. data[threading.currentThread().getName()] = a
  8. if(threading.active_count() == 2): # se só houverem 2 threads esta e a main
  9. kill_sig.put(True)
  10.  
  11. data = {} # armazenar os dados
  12. kill_sig = queue.Queue()
  13. fibos = [10, 15, 20, 30]
  14. for k, v in enumerate(fibos, 1):
  15. t = threading.Thread(target=fibo, args=(v,), name='t_{}'.format(k)).start() # preparar cada thread e comecar trab
  16.  
  17. kill_sig.get() # bloquear aqui ate receber 'sinal'
  18. print(data) # trabalho feito por cada thread
Success #stdin #stdout 0.04s 10208KB
stdin
Standard input is empty
stdout
{'t_1': 55, 't_2': 610, 't_4': 832040, 't_3': 6765}