Created
October 9, 2014 20:56
-
-
Save JuniorPolegato/170bf6254b89f392c2e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import threading | |
import time | |
# Máximo de conexões/threads simultâneas | |
MAX_CONEXOES = 30 | |
# Função para imprimir uma linha por vez via lock | |
print_lock = threading.Lock() | |
def mostrar_msg(msg): | |
print_lock.acquire() | |
print msg | |
print_lock.release() | |
# Função para cada thread | |
def consultar_cpf(cpf): | |
# Código para consuta, aqui vou colocar um tempo de 1s para simular | |
mostrar_msg("Consultando o CPF `%s´..." % cpf) | |
time.sleep(1) | |
mostrar_msg("Terminada consulta do CPF `%s´!" % cpf) | |
# Thread principal | |
lista_threads = [] | |
with open('cpfs.txt', 'rb') as arquivo: | |
for linha in arquivo: | |
cpf = linha.strip() | |
while threading.active_count() > MAX_CONEXOES: | |
mostrar_msg("Esperando 1s...") | |
time.sleep(1) | |
thread = threading.Thread(target=consultar_cpf, args=(cpf,)) | |
lista_threads.append(thread) | |
thread.start() | |
# Esperando pelas threads abertas terminarem | |
mostrar_msg("Esperando threads abertas terminarem...") | |
for thread in lista_threads: | |
thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment