51 lines
1.4 KiB
Python
Executable File
51 lines
1.4 KiB
Python
Executable File
"""
|
|
Threading
|
|
"""
|
|
|
|
from threading import Thread
|
|
import os, time
|
|
|
|
|
|
def nros_cuadrados():
|
|
"""
|
|
función dummy para ser ejecutada por Thread 'target'
|
|
"""
|
|
for i in range(100):
|
|
i * i
|
|
time.sleep(0.1)
|
|
|
|
# lista que guarda todos los threads
|
|
hilos = []
|
|
num_hilos = 10
|
|
|
|
# creación de los hilos
|
|
for i in range(num_hilos):
|
|
# Thread toma 2 argumentos
|
|
# función target
|
|
h = Thread(target=nros_cuadrados) # , args=(tupla_args))
|
|
hilos.append(h)
|
|
|
|
# Iniciar hilos
|
|
for h in hilos:
|
|
h.start()
|
|
|
|
# join (bloquear el hilo principal hasta terminar ejecución de procesos)
|
|
for h in hilos:
|
|
h.join()
|
|
|
|
print("FIN main")
|
|
"""
|
|
Ejemplo de la salida de Htop
|
|
|
|
23903 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23904 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23905 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23906 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23907 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23908 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23909 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23910 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23911 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
23912 root 20 0 8732 S 0.0 0.1 0:00.00 11 python3 b_multit.py
|
|
"""
|