Last active
December 31, 2018 19:25
-
-
Save Jul10l1r4/27b2b4c8ab4005e480514419bc949f5e to your computer and use it in GitHub Desktop.
Usando linhas de execução para realizar requisições de forma veloz.
This file contains 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
import urllib2 | |
from multiprocessing.dummy import Pool as ThreadPool | |
import time | |
# Urls a ser percorrido | |
urls = [ | |
'https://www.google.com', | |
'http://www.python.org/about/', | |
'https://jul10l1r4.github.io', | |
'https://facebook.com', | |
'https://twitter.com', | |
'https://youtube.com', | |
'https://wikipedia.org', | |
'https://wiki.python.org/', | |
] | |
# Faz o pool trabalhar em suas threads | |
pool = ThreadPool(8) | |
# Funcoes que sera percorrida | |
def func (x): | |
print "\033[32m"+x+"\033[0m" | |
urllib2.urlopen(x) | |
# Com as threads definidas chama o "map" | |
results = pool.map(func, urls) | |
# Fecha o pool e aguarda o trabalho finalizar | |
pool.close() | |
pool.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment