|
| 1 | +import asyncio |
| 2 | + |
| 3 | +async def Segundos(nombre, segundos): |
| 4 | + print(f"La tarea '{nombre}' comenzó.") |
| 5 | + print(f"La tarea '{nombre}' durará {segundos} segundos.") |
| 6 | + await asyncio.sleep(segundos) |
| 7 | + print(f"La tarea '{nombre}' finalizó.") |
| 8 | + |
| 9 | + |
| 10 | +async def ejecutar_tareas(): |
| 11 | + nombre_func = input('Nombre de la función: ') |
| 12 | + actividades = [ |
| 13 | + Segundos(nombre_func, 2), |
| 14 | + ] |
| 15 | + # Ejecutar las actividades |
| 16 | + await asyncio.gather(*actividades) |
| 17 | + |
| 18 | +asyncio.run(ejecutar_tareas()) |
| 19 | + |
| 20 | +''' |
| 21 | +EXTRA |
| 22 | +''' |
| 23 | + |
| 24 | +async def funcA(segundos): |
| 25 | + |
| 26 | + await asyncio.sleep(segundos) |
| 27 | + print('La función A ya finalizó') |
| 28 | + |
| 29 | +async def funcB(segundos): |
| 30 | + |
| 31 | + await asyncio.sleep(segundos) |
| 32 | + print('La función B ya finalizó') |
| 33 | + |
| 34 | +async def funcC(segundos): |
| 35 | + |
| 36 | + await asyncio.sleep(segundos) |
| 37 | + print('La función C ya finalizó') |
| 38 | + |
| 39 | +async def funcD(segundos): |
| 40 | + |
| 41 | + await asyncio.sleep(segundos) |
| 42 | + print('La función D ya finalizó') |
| 43 | + |
| 44 | + |
| 45 | +async def FuncsABCD(): |
| 46 | + print('Las funciones A, B y C se estan ejecutandose') |
| 47 | + await asyncio.gather(funcA(1), funcB(2), funcC(3)) |
| 48 | + print('Las funciones A, B y C finalizaron') |
| 49 | + await asyncio.gather(funcD(1)) |
| 50 | + |
| 51 | + |
| 52 | +asyncio.run(FuncsABCD()) |
0 commit comments