|
| 1 | +#15 { Retos para Programadores } ASINCRONÍA |
| 2 | + |
| 3 | +# Bibliography reference |
| 4 | +# Professional JavaScript for web developers by Matt Frisbie [Frisbie, Matt] (z-lib.org) |
| 5 | +#Python Notes for Professionals. 800+ pages of professional hints and tricks (GoalKicker.com) (Z-Library) |
| 6 | +# Additionally, I use GPT as a reference and sometimes to correct or generate proper comments. |
| 7 | + |
| 8 | +""" |
| 9 | +* EJERCICIO: |
| 10 | + * Utilizando tu lenguaje, crea un programa capaz de ejecutar de manera |
| 11 | + * asíncrona una función que tardará en finalizar un número concreto de |
| 12 | + * segundos parametrizables. También debes poder asignarle un nombre. |
| 13 | + * La función imprime su nombre, cuándo empieza, el tiempo que durará |
| 14 | + * su ejecución y cuando finaliza. |
| 15 | + * |
| 16 | + * DIFICULTAD EXTRA (opcional): |
| 17 | + * Utilizando el concepto de asincronía y la función anterior, crea |
| 18 | + * el siguiente programa que ejecuta en este orden: |
| 19 | + * - Una función C que dura 3 segundos. |
| 20 | + * - Una función B que dura 2 segundos. |
| 21 | + * - Una función A que dura 1 segundo. |
| 22 | + * - Una función D que dura 1 segundo. |
| 23 | + * - Las funciones C, B y A se ejecutan en paralelo. |
| 24 | + * - La función D comienza su ejecución cuando las 3 anteriores han finalizado. |
| 25 | + |
| 26 | +""" |
| 27 | + |
| 28 | +import time |
| 29 | +import threading |
| 30 | + |
| 31 | +# Short for print() |
| 32 | +log = print |
| 33 | + |
| 34 | +def run_func(name, seconds): |
| 35 | + log(f"{name} - Start at: {time.strftime('%H:%M:%S')}") |
| 36 | + log(f"{name} - Last: {seconds} seconds") |
| 37 | + |
| 38 | + time.sleep(seconds) |
| 39 | + |
| 40 | + log(f"{name} - Ends at: {time.strftime('%H:%M:%S')}") |
| 41 | + |
| 42 | +def run_funces(): |
| 43 | + # Create threads for each function to run concurrently |
| 44 | + function_c = threading.Thread(target=run_func, args=('Function C', 3)) |
| 45 | + function_b = threading.Thread(target=run_func, args=('Function B', 2)) |
| 46 | + function_a = threading.Thread(target=run_func, args=('Function A', 1)) |
| 47 | + |
| 48 | + # Start all threads |
| 49 | + function_c.start() |
| 50 | + function_b.start() |
| 51 | + function_a.start() |
| 52 | + |
| 53 | + # Wait for all threads to complete |
| 54 | + function_c.join() |
| 55 | + function_b.join() |
| 56 | + function_a.join() |
| 57 | + |
| 58 | + # Run Function D after A, B, and C have completed |
| 59 | + run_func('Function D', 1) |
| 60 | + |
| 61 | +# Simulate the loading of a web page |
| 62 | +time.sleep(2) # Simulate a delay before showing the alert |
| 63 | +log('Retosparaprogramadores #15') |
| 64 | + |
| 65 | +run_funces() |
| 66 | + |
| 67 | +# Output Example: |
| 68 | +""" |
| 69 | +Retosparaprogramadores #15 |
| 70 | +Function C - Start at: 07:27:35 |
| 71 | +Function C - Last: 3 seconds |
| 72 | +Function B - Start at: 07:27:35 |
| 73 | +Function B - Last: 2 seconds |
| 74 | +Function A - Start at: 07:27:35 |
| 75 | +Function A - Last: 1 seconds |
| 76 | +Function A - Ends at: 07:27:36 |
| 77 | +Function B - Ends at: 07:27:37 |
| 78 | +Function C - Ends at: 07:27:38 |
| 79 | +Function D - Start at: 07:27:38 |
| 80 | +Function D - Last: 1 seconds |
| 81 | +Function D - Ends at: 07:27:39 |
| 82 | +
|
| 83 | +""" |
0 commit comments