From 618f5d61560b9c90389fb930a7f18b79ce80ce04 Mon Sep 17 00:00:00 2001 From: javiermengual Date: Mon, 17 Nov 2025 14:53:35 +0100 Subject: [PATCH] Add initial implementation of threading examples in 03.py and 04.py --- py/threads/03.py | 22 ++++++++++++++++++++++ py/threads/04.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 py/threads/03.py create mode 100644 py/threads/04.py diff --git a/py/threads/03.py b/py/threads/03.py new file mode 100644 index 0000000..254b4f8 --- /dev/null +++ b/py/threads/03.py @@ -0,0 +1,22 @@ +import threading +import time +import random + +def tareaUno(): + global Done + time.sleep (random.random()) + if not Done: + print("Tarea realizada") + Done = True + else : + print ("tarea NO REALIZADA") + return + + +Done = False +hilos = list() +for i in range(50): + t = threading.Thread(target=tareaUno) + hilos.append(t) + t.start() +time.sleep(1) \ No newline at end of file diff --git a/py/threads/04.py b/py/threads/04.py new file mode 100644 index 0000000..db2bad4 --- /dev/null +++ b/py/threads/04.py @@ -0,0 +1,38 @@ +import threading +import time +import random + +Done = False +i = 1 +j = 1 + +def tareaUno(): + global Done + global i + if not Done: + print("Tarea %d realizada\n" % i,flush=True) + Done = True + else : + print ("tarea %d NO REALIZADA\n" % i,flush=True) + i +=1 + return + +def tareaDos(): + global Done + global j + Done = False + print("----- %d tareaDos\n" % j,flush=True) + j+=1 + return + +if __name__ == '__main__': + hilos = list() + for i in range(50): + t = threading.Thread(target=tareaUno) + if (i % 5) == 0: + t = threading.Thread(target=tareaDos) + # hilos.append(t) + t.start() + # time.sleep(0.01) + + time.sleep(5) \ No newline at end of file