Skip to content

Instantly share code, notes, and snippets.

@Jul10l1r4
Last active December 31, 2018 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jul10l1r4/27b2b4c8ab4005e480514419bc949f5e to your computer and use it in GitHub Desktop.
Save Jul10l1r4/27b2b4c8ab4005e480514419bc949f5e to your computer and use it in GitHub Desktop.
Usando linhas de execução para realizar requisições de forma veloz.
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