Add initial implementation of threading examples in 03.py and 04.py

This commit is contained in:
javiermengual 2025-11-17 14:53:35 +01:00
parent f7486171bc
commit 618f5d6156
2 changed files with 60 additions and 0 deletions

22
py/threads/03.py Normal file
View File

@ -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)

38
py/threads/04.py Normal file
View File

@ -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)