示例#1
0
def rAopcion1():
    # listaDePersonas =[Persona('juan'),Persona('pedro'),Persona('julia'),Persona('maria')]
    cont = Contador()
    cont.iniciar()
    for persona in listaDePersonas:
        contInterno = Contador()
        contInterno.iniciar()
        th = threading.Thread(target=persona.correr, args=[contInterno])
        th.start()

    cont.finalizar()
    cont.imprimir()
logging.basicConfig(format='%(asctime)s.%(msecs)03d [%(threadName)s] - %(message)s', datefmt='%H:%M:%S', level=logging.INFO)

# la función para usar para el thread
def dormir():
    time.sleep(1)

def dormirEj2(segundos):
    time.sleep(segundos)

contador = Contador()
contador.iniciar()
"""Ejercicio 1"""

for i in range(10):
    thread = threading.Thread(target=dormir)
    thread.start()
"""

hilos = []

for i in range(10):
    thread = threading.Thread(target=dormirEj2, args=[1])
    hilos.append(thread)
    thread.start()
    thread.join()

"""
    
contador.finalizar()
contador.imprimir()
    'https://images.unsplash.com/photo-1507143550189-fed454f93097',
    'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e',
    'https://images.unsplash.com/photo-1504198453319-5ce911bafcde',
    'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99',
    'https://images.unsplash.com/photo-1516972810927-80185027ca84',
    'https://images.unsplash.com/photo-1550439062-609e1531270e',
    'https://images.unsplash.com/photo-1549692520-acc6669e2f0c'
]


def bajar_imagen(img_url):
    img_bytes = requests.get(img_url).content
    img_name = img_url.split('/')[3]
    img_name = f'{img_name}.jpg'
    with open(img_name, 'wb') as img_file:
        img_file.write(img_bytes)
        # print(f'{img_name} fue bajada...')


tiempo = Contador()

tiempo.iniciar()

# una por una
for url in img_urls:
    threading.Thread(target=bajar_imagen(url)).start()

tiempo.finalizar()
tiempo.imprimir()

# Pero ahora con threads
import threading
import time
import logging
from tiempo import Contador

logging.basicConfig(format='%(asctime)s.%(msecs)03d [%(threadName)s] - %(message)s', datefmt='%H:%M:%S', level=logging.INFO)
cont = Contador()

# la función para usar para el thread
def dormir(secs):
    time.sleep(secs)

cont.iniciar()

lista = []

for i in range(10):
    th = threading.Thread(target=dormir, args=[1.5], name='thread desde un for')
    th.start()
    lista.append(th)
    #crear un thead
    #lanzarlo

for hilo in lista:
    hilo.join()

cont.finalizar()
cont.imprimir()
def bajar_imagen(img_url):
    logging.info('bajar_imagen')
    img_bytes = requests.get(img_url).content
    img_name = img_url.split('/')[3]
    img_name = f'{img_name}.jpg'
    with open(img_name, 'wb') as img_file:
        img_file.write(img_bytes)
        # print(f'{img_name} fue bajada...')


tiempo = Contador()

tiempo.iniciar()

# una por una
""" for url in img_urls:
    bajar_imagen(url)

tiempo.finalizar()
tiempo.imprimir() """

# Pero ahora con threads

for url in img_urls:
    t1 = threading.Thread(target=bajar_imagen(url), name='descargando imagen')
    t1.start()
    #t1.join()

tiempo.finalizar()
tiempo.imprimir()
        #print(f'{img_name} fue bajada...') de onda queda horrible con esto.


tiempoConThreads = Contador()
tiempoConThreads.iniciar()

for url in img_urls:
    url = threading.Thread(target=bajar_imagen, args=[url])
    url.start()
    #url.join() con join el tiempo es similar al tiempo sin threads, pero sin el contador imprime 0.04(+o-)

printThread = threading.Thread(
    target=print,
    args=[
        "Que onda mamamasa. Texto para saber si imprime esto mientras sigue descargando las imagenes y lo al final"
    ])
printThread.start()
tiempoConThreads.finalizar()
tiempoConThreads.imprimir()

# una por una Al rededor de 60´´
"""
tiempo = Contador()
tiempo.iniciar()

for url in img_urls:
    bajar_imagen(url)

tiempo.finalizar()
tiempo.imprimir()
"""
示例#7
0
    time.sleep(1)


#forma hecha clasica por nosotros
'''tiempo=0
for i in range(10):
    t=Contador()
    t.iniciar()
    i=threading.Thread(target=dormir , name= "tread desde funcion")
    i.start()
    i.join()
    t.finalizar()
    t.imprimir()
    tiempo+=t.numero()
print('pasaron en total',tiempo,'segundos')'''

#forma del profesor, mucho mas simple

y = Contador()
y.iniciar()
lista = []  #lista vacia
for i in range(10):
    i = threading.Thread(target=dormir, name="tread desde funcion")
    i.start()
    lista.append(i)
    #i.join()
for thread in lista:
    thread.join()
y.finalizar()
y.imprimir()