16 de noviembre de 2016

Threads multitarea

Vamos a ver un ejemplo en el que tenemos 3 tipos de tareas a realizar (task1, task2, task3) y queremos arrancar un determinado número de threads o hilos para ejecutar cada una de ellas.
Para ello vamos a definir un diccionario THREADS = { 'task1':2 , 'task2':3, 'task3':2} donde indicamos el número de threads destinado a cada tarea.
El código sería algo del siguiente tipo:
#!/usr/bin/env python

import threading
import random
import time

# how many threads we want to start
THREADS = { 'task1':2 , 'task2':3, 'task3':2}

def task1(threadName):
    while True:
        print "I am %s and I execute task1" % threadName
        time.sleep(random.randint(1, 10))

def task2(threadName):
    while True:
        print "I am %s and I execute task2" % threadName
        time.sleep(random.randint(1, 10))

def task3(threadName):
    while True:
        print "I am %s and I execute task3" % threadName
        time.sleep(random.randint(1, 10))

def generic_workflow(threadName, task_type):
    if task_type == 'task1':
        task1(threadName)
    elif task_type == 'task2':
        task2(threadName)
    elif task_type == 'task3':
        task3(threadName)

class Thread_task(threading.Thread):
    def __init__(self, task_type):
        threading.Thread.__init__(self)
        self.task_type = task_type
    def run(self):
        threadName = threading.currentThread().getName()
        generic_workflow(threadName, task_type)

print 'Checking for threads for every task...'

total_threads = 0
for task in THREADS:
    total_threads += THREADS[task]

for task in THREADS:
    print " ** Starting %d threads for %s **" % (THREADS[task], task)
    for i in range(THREADS[task]):
        task_type = task
        td = Thread_task(task)
        td.start()
La ejecución del código generaría una salida del siguiente tipo:
Checking for threads for every task...
 ** Starting 2 threads for task1 **
I am Thread-1 and I execute task1
I am Thread-2 and I execute task1
 ** Starting 3 threads for task2 **
I am Thread-3 and I execute task2
I am Thread-4 and I execute task2
I am Thread-5 and I execute task2
 ** Starting 2 threads for task3 **
I am Thread-6 and I execute task3
I am Thread-7 and I execute task3
I am Thread-1 and I execute task1
I am Thread-7 and I execute task3
I am Thread-3 and I execute task2
I am Thread-1 and I execute task1
I am Thread-6 and I execute task3
........

Resultará fácil para el lector adaptar el código para hacer algo similar con procesos en lugar de threads.

No hay comentarios:

Publicar un comentario