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) # 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()
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()
'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
'https://images.unsplash.com/photo-1550439062-609e1531270e', 'https://images.unsplash.com/photo-1549692520-acc6669e2f0c' """ ] 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()
'https://images.unsplash.com/photo-1549692520-acc6669e2f0c' ] downloadDirectory = 'img\ ' 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(downloadDirectory + img_name, 'wb') as img_file: img_file.write(img_bytes) #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()
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()
import requests import time import logging import threading from tiempo import Contador logging.basicConfig( format='%(asctime)s.%(msecs)03d [%(threadName)s] - %(message)s', datefmt='%H:%M:%S', level=logging.INFO) tiempo = Contador() ctdThreads = 10 threads = [] img_urls = [ 'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759', 'https://images.unsplash.com/photo-1532009324734-20a7a5813719', 'https://images.unsplash.com/photo-1524429656589-6633a470097c', 'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79', 'https://images.unsplash.com/photo-1564135624576-c5c88640f235', 'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6', 'https://images.unsplash.com/photo-1522364723953-452d3431c267', 'https://images.unsplash.com/photo-1513938709626-033611b8cc03', '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',
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()