mirror of https://github.com/JavMB/ejercicios
Add initial implementation of threading examples in 03.py and 04.py
This commit is contained in:
parent
f7486171bc
commit
618f5d6156
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
Loading…
Reference in New Issue